c++cmakeshared-librariesstatic-libraries

How to create a shared library wrapper for a static library (without using source files) with CMake?


I'd like to create a liboo.so from a libfoo.a using CMake.

So far I have

include_directories(third-party/includes)
find_library(${thirdparty_LIBRARIES} foo PATHS third-party/lib)
add_library(boo SHARED empty.cpp)
target_link_libraries(boo ${thirdparty_LIBRARIES})
add_executable(runBoo main.cpp)
target_link_libraries(runBoo boo)

where main calls functions from libfoo.so. But provokes the error:

main.cpp:(.text+0x26): undefined reference to `Foo::Foo()'
main.cpp:(.text+0x50): undefined reference to `Foo::sayHello(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'

I'm guessing the symbols aren't added since empty.cpp doesn't use them, but I'm not sure that's the issue and how to overcome it.

I've seen CMake: how create a single shared library from all static libraries of subprojects? , but I'd prefer to stick to lower versions of cmake for now.

I've also seen how to link static library into dynamic library in gcc but I can't get it to work and I'm using CMake anyway.


Solution

  • There was just a silly mistake. The corrected code is:

       include_directories(third-party/includes)
    
       find_library(thirdparty_LIBRARIES foo PATHS third-party/lib) //remove the ${}!
    
       add_library(boo SHARED empty.cpp)
       target_link_libraries(boo ${thirdparty_LIBRARIES})
       add_executable(runBoo main.cpp)
       target_link_libraries(runBoo boo)