0

I am trying to understand how to manage multiple projects with cmake and I used https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/How-to-create-a-ProjectConfig.cmake-file as a start I would like to create a project that can include the lib Foo (from the link)

The example is compiled correctly and I tried to add a project to the FooBar directory called "app"

FooBar/app/

├── CMakeLists.txt
└── src
    ├── app.cpp

CMakeLists.txt

project(app)
find_package(FooBar)
add_executable(app
    src/app.cpp)
target_link_libraries(app ${FOOBAR_LIBRARIES})

app.cpp

#include<foo/foo.h>

int main(int argc, char** argv){
    foo();
    return 0;
}

The error I received is app.cpp:(.text+0x10): undefined reference to `foo()'

It seems that the find_package and the import of the header foo.h work fine. but for some reason, the library seems doesn't seem to be linked.

I tried to just append the CMakeLists.txt of the app project to the FooBar CMakelists.txt with add_subdirectory(app) or to run the cmake and make cmd in a completely different directory and both have the same result

I was expecting it to work within the same CMakelists or in another directory with the environment variable export CMAKE_PREFIX_PATH=/path/to/FooBar/build:${CMAKE_PREFIX_PATH}

Anyone would have an idea about what is wrong in my case? Thanks

GuillaumeB
  • 99
  • 9
  • If you suspect that something wrong with your `FooBar` library, then show its `CMakeLists.txt`. And show how the library **defines** the `foo()` function. (The function is defined not in the *header* file, which just declares it, but in the library's **source file**). – Tsyvarev Jan 12 '19 at 16:39
  • All the cmake files are described the link https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/How-to-create-a-ProjectConfig.cmake-file And the foo.h just define a simple function `void foo(void);` – GuillaumeB Jan 12 '19 at 16:42
  • What content of `FOOBAR_LIBRARIES` variable, which you use in your application's `CMakeLists.txt`, is? You may print value of the variable with `message()` command. – Tsyvarev Jan 13 '19 at 10:21
  • -- FOOBAR_INCLUDE_DIRS = path/to/FooBar;path/to/FooBar/build ; -- FOOBAR_LIBS = foo ; both seems to be correct – GuillaumeB Jan 13 '19 at 19:12
  • So, your problem is completely resolve to that answer to duplicate question: https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix/12574420#12574420. – Tsyvarev Jan 13 '19 at 22:35

1 Answers1

0

I just found out why the linking was not working. The example provided by cmake is a c library and my package is a cpp app. So I changed app.cpp by

extern "C"{
    #include<foo/foo.h>
}

int main(int argc, char** argv){

    //foo();
    foo();
    return 0;
}

and everything works. My problem was not a cmake problem at all it was just that I was trying to include a c lib into a c++ app.

GuillaumeB
  • 99
  • 9