0

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.
Brian Erickson
  • 945
  • 8
  • 18
  • 1
    There isnt anything c++14 specific in your code/error output. Can you make with verbosity and make sure the g++ compiler is actually being used and the standard c++ library (-lstdc++) is being linked? make VERBOSE=1 – partyd Apr 14 '17 at 16:15
  • OK, output includes `/usr/bin/c++ -I/home/brian/Dropbox/example/. -Wall -std=gnu++14 -o CMakeFiles/example.dir/main.cpp.o -c /home/brian/Dropbox/example/main.cpp' – Brian Erickson Apr 14 '17 at 16:22
  • I don't know how to post the entire output because of comment character limits. – Brian Erickson Apr 14 '17 at 16:23
  • Also, that was from the command line, I don't know how to do the same thing from QT. – Brian Erickson Apr 14 '17 at 16:24
  • You can edit your question and add the output into the question. Beside relative paths like . should not be used in CMakeLists.txt. Use CMAKE_SOURCE_DIR instead to support out of tree builds – arved Apr 15 '17 at 11:54
  • update: changing compiler in kit from gcc to clang makes it start working, but that isn't the answer I was looking for, should work with gcc (g++?). – Brian Erickson Apr 26 '17 at 03:18
  • 2
    You can add `VERBOSE=1` under Projects > Build Steps > Tool Arguments. This would enable you to compare command line with QtCreator, which may give you a clue to the solution. – m7913d Apr 26 '17 at 08:04
  • 1
    What is the *link* command generated by cmake? Instead of writing a comment you could also update the question to give the full VERBOSE=1 compile output. – Gert Wollny Apr 26 '17 at 08:16
  • Thanks m7913d for instructions on updating the verbose option in QT. I have updated the build output with the VERBOSE=1 version. – Brian Erickson Apr 26 '17 at 15:45
  • 1
    You are trying to use the gcc command as a linker for a c++ program. As explained in about one zillion and one places, this cannot work. Use the g++ command instead. Will close as a dupe in 3...2...1... – n. m. could be an AI Apr 26 '17 at 16:02
  • Apparently questions with bounties cannot be closed, anyway here's a dupe (one of many): http://stackoverflow.com/questions/28236870/undefined-reference-to-stdcout – n. m. could be an AI Apr 26 '17 at 16:13
  • Thanks n.m. I think that is closer to an answer, but it isn't the answer I was looking for. The question is why with the same steps, the same files are working in one environment and not in the other. It looks like QT is trying to link with GCC, but why? In neither case did I select to use gcc over g++ or vice versa, maybe that's something QT is doing behind my back or something that is incorrectly configured? – Brian Erickson Apr 26 '17 at 18:41
  • 1
    Can you check the chosen compiler for the kit you are using. You can check it under `Projects > Manage Kits...`. Select your kit and check the compiler option. Under the tab `Compilers`, you can check the full path corresponding with this compiler. – m7913d Apr 27 '17 at 06:56

2 Answers2

2

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.

Community
  • 1
  • 1
pe3k
  • 796
  • 5
  • 15
  • 1
    That's it. Changing the kit to g++ was a start, then I went to the build settings / advanced and changed CMAKE_CXX_COMPILER to /usr/bin/g++ and that solved it. – Brian Erickson Apr 27 '17 at 14:36
0

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.

Marek R
  • 32,568
  • 6
  • 55
  • 140