CMake:从src和其他静态库构建共享库

时间:2018-05-05 12:06:08

标签: c++ cmake shared-libraries static-libraries

\- lib
  CMakeLists.txt 
  libA.a
  libB.a
  \- src 
    \- mod1
    \- mod2
  \- inc
    \- mod1
    \- mod2
    \- libA
    \- libB
\- examples
  \- common_inc // common includes for all examples
  \- my_executable
    CMakeLists.txt 
    \- src 
    \- inc

src包含 mod1和mod2的.cpp文件,两者都依赖于libA,libB ,inc包含libA,libB以及​​mod1和mod2的头文件

问题是:

我想创建一个包含libA.a,libB.a以及我的src(mod1和mod2)的共享库,我该如何实现?

除了从mod1和mod2创建静态库(让我们称之为libMy)之外还有其他方法,然后将所有3个库(libMy,libA,libB)链接到一个共享库中吗?

我想出了以下CMakeLists.txt,但找不到-llibA和-llibB

cmake_minimum_required (VERSION 3.5)

project (libMy CXX)
set (CMAKE_CXX_STANDARD 14)

set (SRC
                "src/mod1/a1.cpp"
                "src/mod2/a2.cpp"
)

set (INC
                "inc/mod1/a1.hpp"
                "inc/mod2/a2.hpp"
                "inc/lib1/libA.hpp"
                "inc/lib2/libB.hpp"

)
add_library(libMy STATIC ${SRC} ${INC})
target_include_directories(libMy PUBLIC inc)

add_library(master_library SHARED
    dummy.cpp
)
target_link_libraries(master_library libMy)
target_link_libraries(master_library -lpthread)
target_link_libraries(master_library libA.a)
target_link_libraries(master_library libB.a)

更新

感谢@Tsyvarev我设法解决了这个问题。但还有另外一个......

当我尝试将我的共享库链接到my_executable时,我得到未定义的引用错误,这里是完整的错误消息:

../../../lib/libmylib.a(session.cpp.o): In function `mylib::network::session::write(mylib::network::packet const&)':
session.cpp:(.text+0x4e5): undefined reference to `libA::tcp_client::async_write(libA::tcp_client::write_request const&)'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::tcp_server()':
tcp_server.cpp:(.text+0x24b): undefined reference to `libA::tcp_server::tcp_server()'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::~tcp_server()':
tcp_server.cpp:(.text+0x323): undefined reference to `libA::tcp_server::~tcp_server()'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::start(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)::{lambda(std::shared_ptr<libA::tcp_client> const&)#1}::operator()(std::shared_ptr<libA::tcp_client> const&) const':
tcp_server.cpp:(.text+0x445): undefined reference to `libA::tcp_client::async_read(libA::tcp_client::read_request const&)'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::start(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
tcp_server.cpp:(.text+0x52c): undefined reference to `libA::tcp_server::start(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, std::function<bool (std::shared_ptr<libA::tcp_client> const&)> const&)'
../../../lib/libmylib.a(tcp_server.cpp.o): In function `mylib::network::tcp_server::on_new_message(std::shared_ptr<libA::tcp_client> const&, libA::tcp_client::read_result const&)':
tcp_server.cpp:(.text+0x767): undefined reference to `libA::tcp_client::async_read(libA::tcp_client::read_request const&)'
tcp_server.cpp:(.text+0x801): undefined reference to `libA::tcp_client::disconnect(bool)'
../../../lib/libmylib.a(pool.cpp.o): In function `mylib::libB::pool::~pool()':
pool.cpp:(.text+0x42): undefined reference to `ConnectionPool_free'
pool.cpp:(.text+0x5f): undefined reference to `URL_free'
../../../lib/libmylib.a(pool.cpp.o): In function `mylib::libB::pool::connect(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
pool.cpp:(.text+0x249): undefined reference to `URL_new'
pool.cpp:(.text+0x26a): undefined reference to `ConnectionPool_new'
pool.cpp:(.text+0x284): undefined reference to `Exception_stack'
pool.cpp:(.text+0x28b): undefined reference to `pthread_getspecific'
pool.cpp:(.text+0x298): undefined reference to `Exception_stack'
pool.cpp:(.text+0x2a9): undefined reference to `pthread_setspecific'
pool.cpp:(.text+0x2e3): undefined reference to `ConnectionPool_start'
pool.cpp:(.text+0x2f8): undefined reference to `Exception_stack'
pool.cpp:(.text+0x2ff): undefined reference to `pthread_getspecific'
pool.cpp:(.text+0x30c): undefined reference to `Exception_stack'
pool.cpp:(.text+0x316): undefined reference to `pthread_setspecific'
pool.cpp:(.text+0x325): undefined reference to `SQLException'
pool.cpp:(.text+0x39d): undefined reference to `Exception_throw'
../../../lib/libmylib.a(connection.cpp.o): In function `mylib::libB::connection::connection(ConnectionPool_S*)':
connection.cpp:(.text+0x18): undefined reference to `ConnectionPool_getConnection'
../../../lib/libmylib.a(connection.cpp.o): In function `mylib::libB::connection::~connection()':
connection.cpp:(.text+0x41): undefined reference to `Connection_close'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::query(Connection_S*, char const*)':
query.cpp:(.text+0x28): undefined reference to `Connection_prepareStatement'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::set(int)':
query.cpp:(.text+0x93): undefined reference to `PreparedStatement_setInt'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::set(char const*)':
query.cpp:(.text+0xe3): undefined reference to `PreparedStatement_setString'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::get_int()':
query.cpp:(.text+0x15d): undefined reference to `ResultSet_getInt'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::get_str[abi:cxx11]()':
query.cpp:(.text+0x1ee): undefined reference to `ResultSet_getString'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::execute_query()':
query.cpp:(.text+0x28e): undefined reference to `PreparedStatement_executeQuery'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::execute()':
query.cpp:(.text+0x2e4): undefined reference to `PreparedStatement_execute'
query.cpp:(.text+0x2f3): undefined reference to `PreparedStatement_rowsChanged'
../../../lib/libmylib.a(query.cpp.o): In function `mylib::libB::query::next()':
query.cpp:(.text+0x326): undefined reference to `ResultSet_next'
collect2: error: ld returned 1 exit status
CMakeFiles/my_executable.dir/build.make:121: recipe for target 'my_executable' failed
make[2]: *** [my_executable] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/my_executable.dir/all' failed
make[1]: *** [CMakeFiles/my_executable.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

/lib/CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)

project (my_executable CXX)
set (CMAKE_CXX_STANDARD 14)

set (SRC
                "src/main.cpp"
                "src/mysrcA.cpp"
)

set (INC
                "inc/mysrcA.hpp"
)

add_library(mylib STATIC IMPORTED GLOBAL)
set_target_properties(mylib PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/../../lib/mylib.so"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/../../lib/inc"
)


add_executable (my_executable ${SRC})
target_include_directories(dbserver PRIVATE "${CMAKE_SOURCE_DIR}/inc")
target_include_directories(dbserver PRIVATE "${CMAKE_SOURCE_DIR}/../common_inc")

target_link_libraries(my_executable mylib)

/examples/my_executable/CMakeLists.txt:

cmake_minimum_required (VERSION 3.5)

project (mylib CXX)
set (CMAKE_CXX_STANDARD 14)

set (SRC
                "src/mod1/mod1A.cpp"
                "src/mod2/mod2A.cpp"
)

set (INC
                "inc/mod1/mod1A.hpp"
                "inc/mod2/mod2A.hpp"
                "inc/libA/libA.hpp"
                "inc/libB/libB.h"
)

add_library(mylib STATIC ${SRC} ${INC})
target_include_directories(mylib PUBLIC ${CMAKE_SOURCE_DIR}/inc)

add_library(libA STATIC IMPORTED)
set_target_properties(libA PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/libA.a"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/inc/libA"
)

add_library(libB STATIC IMPORTED)
set_target_properties(libB PROPERTIES 
    IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/res/libB.a"
    INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/inc/libB.h"
)
#)
target_link_libraries(mylib libA libB -lpthread)

0 个答案:

没有答案
相关问题