1

I have a very simple project structure and I'm not being able to compile with CMake. I've tried to read documentation about CMake or tutorials, but I can't get it to work.

There's a similar question, but even trying what the answers suggested I can't get it to work.

You can see my full code here

But the relevant CMakeLists are:

root level:

cmake_minimum_required(VERSION 3.5)

find_package(Catch2 REQUIRED)

project(majorityQueries LANGUAGES CXX VERSION 0.0.1)

include_directories(include)

add_subdirectory(src)
add_subdirectory(tests)

file(GLOB SOURCES "*.cpp")

src:

add_library(fact factorial.cpp)

tests:

add_executable(test test_factorial.cpp)

target_link_libraries(test Catch2::Catch2)

But basically I have a test_factorial.cpp file, that includes the header factorial.hpp (inside an include directory) and therefore should know about the existence of the Factorial(int) function, but it says it undefined.

What I try is:

cd build/
cmake ..
make

I expected the make to work, instead I get:

Undefined symbols for architecture x86_64:
  "Factorial(int)", referenced from:
      ____C_A_T_C_H____T_E_S_T____0() in test_factorial.cpp.o
  "Catch::NameAndTags::NameAndTags(Catch::StringRef const&, Catch::StringRef const&)", referenced from:
      ___cxx_global_var_init in test_factorial.cpp.o
  "Catch::StringMaker<int, void>::convert(int)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > Catch::Detail::stringify<int>(int const&) in test_factorial.cpp.o (...)
  • Make sure that the catch2 libraries are not for 32bit architecture but for 64bit. – serkan.tuerker Jul 13 '19 at 21:28
  • I fetched your sources from github and got the same build errors. The CMakeLists.txt of tests directory was not complete. I added source file `test_main.cpp` to `add_executable` entry and library `fact` to `target_link_libraries` entry and now it works. – Mathias Schmid Jul 14 '19 at 01:59
  • Welcome to Stack Overflow! Here we expect the relevant **code in the question post** itself. Please, add this code. "Relevant code" doesn't mean dump of the project. In you case you may provide `CMakeLists.txt` which creates and links the test executable, which cause the error. You may provide a link to the full project, but only as an *addition* to the code in the question, not as a *replacement* for it. – Tsyvarev Jul 14 '19 at 08:49
  • @MathiasSchmid I didn't fully get the part of the library `fact` to target_link_libraries. Where should I declare such a library? – Bernardo Subercaseaux Jul 15 '19 at 02:14
  • @Tsyvarev thanks! I just was unsure about which file really mattered, and I thought it was a lot to post them all. For example it seems that at the end the problem wasn't on the main `CMakeLists.txt` but on one of the subdirectories... And in the question I cited in the original post part of the issue was in the way headers were referred to from src files. But I'll try to post what I believe relevant next time – Bernardo Subercaseaux Jul 15 '19 at 02:16
  • "But I'll try to post what I believe relevant next time" - Do not postpone for the next questions, fix the current one. "For example it seems that at the end the problem wasn't on the main `CMakeLists.txt` but on one of the subdirectories." - You create the test (which causes the error you show) in the subdirectory, so it is natural that `CMakeLists.txt` in that subdirectory is relevant to the problem. – Tsyvarev Jul 15 '19 at 07:49
  • @Tsyvarev I added the CMakeLists to this question. – Bernardo Subercaseaux Jul 27 '19 at 21:25

1 Answers1

0

As i already wrote in my comment, add the source file test_main.cpp and library fact to ./tests/CMakeLists.txt.

add_executable(test test_main.cpp test_factorial.cpp)
target_link_libraries(test fact Catch2::Catch2)
Mathias Schmid
  • 431
  • 4
  • 7
  • Thanks for your answer Mathias, that actually worked! But I think I still don't understand the generality: I did a small change to the project, started doing what I actually wanted to do instead of the factorial example, and got the same error again, and now I am adding the executable and linking the library... if you want to take a look it's the latest commit in https://github.com/bsubercaseaux/MajorityQueries – Bernardo Subercaseaux Jul 27 '19 at 21:41
  • I created an issue for your project on github. – Mathias Schmid Jul 28 '19 at 23:19