简体   繁体   中英

QT Creator CMakeLists.txt c++ link errors

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.

I am running QT Creator 4.2.1 and trying to compile a c++ project defined with CMakeLists.txt. The program compiles fine from command line with cmake . && make cmake . && make . I would like to work from the QT Creator IDE.

I have two Ubuntu machines. One where this process works fine, one where it fails. On the machine where it fails, if I open CMakeLists.txt as a project in QT and try to compile, it fails to compile with many linker errors. How can I fix this?

Here are things I have tried

  • Uninstall and reinstall QT Creator
  • Uninstall and reinstall build-essential
  • Including various lib paths in may makefile
  • Enabling and disabling the system environment variables in the QT project
  • Changing the QT kit to use clang. This works, but I would to understand why gcc isn't working.

I am looking hard for environmental differences but can't figure out what is causing the problem.

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.

It seems your QT Creator is not set right. You are using CMake build system in your project, which uses various environment variables (useful list and description here: https://cmake.org/Wiki/CMake_Useful_Variables ).

When you set compiler (or may be done automatically during install, previous versions forgotten config files, etc.) in your QTCreator settings (eg. see here Qt Creator use another GCC Version located in another Place ), environment variables for your project are set according to QTCreator settings. This may be the difference you get when compiling your project directly. Reset this to correct compiler path (eg. /usr/bin/g++).

You can add output of all CMake variables (eg. see this howto CMAKE: Print out all accessible variables in a script ) and try to compile your projects manually and in QTCreator. From the output you get, the difference should be obvious. I would pay attention to the CMAKE_CXX_COMPILER variable.

This error means that you do not link standard library to your executable.

try add to CMakeLists.txt

TARGET_LINK_LIBRARIES(example stdc++)

Didn't tried so it is possible that this can look a bit different. Just google how to link standard c++ libraries to your target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM