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
. 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.