1

There's zillions of questions on this, but nothing I have read has helped me to resolve my problem.

I am working on unit testing on a large project, so there are multiple CMakeLists.txt files. I created a "utilities.cpp" file which grew quickly, so I decided to split it into test_utilities.cpp and test_utilities.hpp. When I #include test_utilities.cpp there is joy, but if I #include test_utilities.hpp the build fails:

Linking CXX executable foo
CMakeFiles/foo.dir/src/foo.cpp.o: In function `foo::bar()':
/mydir/trunk/tests/src/foo.cpp:57: undefined reference to `exit_on_invalid_config(std::vector<std::basic_string<char, std::char_traits<char>,     std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)'
collect2: ld returned 1 exit status
make[2]: *** [tests/foo] Error 1
make[1]: *** [tests/CMakeFiles/foo.dir/all] Error 2
make: *** [all] Error 2

I think this is because the hpp and cpp are not being linked.

I have tried adding this to CMakeLists.txt:

ADD_EXECUTABLE(utilities src/test_utilities.cpp src/test_utilities.hpp)
TARGET_LINK_LIBRARIES(utilities
    some_project
    test_main
    boost_program_options)

(actually this was here before, minus the hpp part, when I was including utilities.cpp, before I added the header file). This change does not help, so I tried adding

ADD_LIBRARY(test_utilities src/test_utilities.cpp src/test_utilities.hpp)

and included test_utilities into TARGET_LINK_LIBRARIES:

ADD_EXECUTABLE(foo src/foo.cpp)                                                   
TARGET_LINK_LIBRARIES(foo
    some_project
    test_main
    test_utilities
    boost_program_options)

But when I try this, I get errors about boost crap being redefined. When I remove boost imports from foo.cpp, test_utilities.cpp/hpp I either get the same output or complaints that it can't find boost definitions.

What I would like CMake to do is compile the test_utilities executable (cpp + hpp) so that I don't get undefined reference when I try to import test_utilities.hpp from foo.cpp.

Thanks in advance for your help.

Edit: another issue I am having is, I'll add the executable test_utilities, but it doesn't get built before foo. I feel like this is part of my problem and I haven't figured out how to get test_utilities to build first.

Edit: Resolved thanks to wojciii! My issue boiled down to, I was importing boost unit test in test_utilities.cpp, test_main.cpp, and foo.cpp. I needed to stop doing that, fix dependency issues in my hpp, and tweak CMakeLists.txt a little.

5
  • "What I would like CMake to do is compile test_utilities.cpp, link it with test_utilities.hpp, and import test_utilities.hpp from foo.cpp without the undefined reference crap." - You do not link with a header file. That makes no sense. I suggest that you try to edit your question and explain what it is that you want help with. :)
    – wojciii
    Jun 19, 2014 at 19:30
  • Ok, I have my terminology wrong. It's worth mentioning I've only got a few months experience in C++. I thought the whole thing was, you compile the cpp to a .o, the hpp to a .o, and link them into an executable???
    – Aaron
    Jun 19, 2014 at 19:33
  • That is usually what you need to do and it works out of the box. Create a static library. Add an executable. Link the executable with the static library. This should give you a working executable.
    – wojciii
    Jun 19, 2014 at 19:35
  • do you have some example code I can follow? I'm not sure what I should be doing that I'm not doing. Thanks again by the way.
    – Aaron
    Jun 19, 2014 at 19:41
  • 1
    How about github.com/jameskbride/cmake-hello-world ?
    – wojciii
    Jun 19, 2014 at 19:55

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.