Note: I have updated this from the original post to simplify, clarify, and reflect things I have already tried, including adopting some suggestions from comments.
我正在运行QT Creator 4.2.1并尝试编译使用CMakeLists.txt定义的c项目 . 该程序使用 cmake . && make
从命令行编译 . 我想在QT Creator IDE中工作 .
我有两台Ubuntu机器 . 这个过程正常工作的地方,一个失败的地方 . 在它失败的机器上,如果我在QT中打开CMakeLists.txt作为项目并尝试编译,则无法编译许多链接器错误 . 我怎样才能解决这个问题?
这是我尝试过的东西
-
卸载并重新安装QT Creator
-
卸载并重新安装build-essential
-
在may makefile中包含各种lib路径
-
在QT项目中启用和禁用系统环境变量
-
更改QT套件以使用clang . 这有效,但我会理解为什么gcc不起作用 .
我正在努力寻找环境差异,但无法弄清楚导致问题的原因 .
main.cpp :
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello, QT!" << std::endl;
}
CMakeLists.txt :
project(example)
cmake_minimum_required(VERSION 2.8)
add_executable(example main.cpp)
Compile Output
08:42:09: Running steps for project example...
08:42:09: Starting: "/usr/bin/cmake" --build . --target all -- VERBOSE=1
/usr/bin/cmake -H/home/brian/example -B/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug/CMakeFiles /home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug/CMakeFiles/progress.marks
/usr/bin/make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug'
/usr/bin/make -f CMakeFiles/example.dir/build.make CMakeFiles/example.dir/depend
make[2]: Entering directory '/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug'
cd /home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/brian/example /home/brian/example /home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug /home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug /home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug/CMakeFiles/example.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug'
/usr/bin/make -f CMakeFiles/example.dir/build.make CMakeFiles/example.dir/build
make[2]: Entering directory '/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug'
[ 50%] Building CXX object CMakeFiles/example.dir/main.cpp.o
/usr/bin/gcc -g -o CMakeFiles/example.dir/main.cpp.o -c /home/brian/example/main.cpp
[100%] Linking CXX executable example
/usr/bin/cmake -E cmake_link_script CMakeFiles/example.dir/link.txt --verbose=1
/usr/bin/gcc -g CMakeFiles/example.dir/main.cpp.o -o example -rdynamic
CMakeFiles/example.dir/main.cpp.o: In function `main':
/home/brian/example/main.cpp:6: undefined reference to `std::cout'
/home/brian/example/main.cpp:6: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/home/brian/example/main.cpp:6: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
CMakeFiles/example.dir/build.make:94: recipe for target 'example' failed
make[2]: Leaving directory '/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug'
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/example.dir/all' failed
make[1]: Leaving directory '/home/brian/build-example-Desktop_Qt_5_8_0_GCC_64bit-Debug'
Makefile:83: recipe for target 'all' failed
/home/brian/example/main.cpp:6: undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
CMakeFiles/example.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
/usr/include/c++/6/iostream:74: undefined reference to `std::ios_base::Init::Init()'
/usr/include/c++/6/iostream:74: undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
make[2]: *** [example] Error 1
make[1]: *** [CMakeFiles/example.dir/all] Error 2
make: *** [all] Error 2
08:42:09: The process "/usr/bin/cmake" exited with code 2.
Error while building/deploying project example (kit: Desktop Qt 5.8.0 GCC 64bit)
When executing step "Make"
08:42:09: Elapsed time: 00:00.
2 回答
看来您的QT Creator设置不正确 . 您正在项目中使用CMake构建系统,该系统使用各种环境变量(此处有用的列表和说明:https://cmake.org/Wiki/CMake_Useful_Variables) .
当您在QTCreator设置中设置编译器(或可能在安装期间自动完成,以前的版本忘记配置文件等)时(例如,请参见此处Qt Creator use another GCC Version located in another Place),您的项目的环境变量将根据QTCreator设置进行设置 . 这可能是您直接编译项目时遇到的差异 . 将其重置为正确的编译器路径(例如/ usr / bin / g) .
您可以添加所有CMake变量的输出(例如,请参见此howto CMAKE: Print out all accessible variables in a script)并尝试在QTCreator中手动编译项目 . 从你得到的输出,差异应该是显而易见的 . 我会注意CMAKE_CXX_COMPILER变量 .
此错误意味着您不将标准库链接到可执行文件 .
尝试添加到
CMakeLists.txt
没试过,这可能看起来有点不同 . 只需谷歌如何将标准c库链接到您的目标 .