简体   繁体   中英

using static libraries in cmake project

I have two C++ projects A and B. Project B depends on project A. Project A has this structure split into some subdirectories:

Project A
|-\inc
|  |-a1.h
|  |-a2.h
|-\src
   |-CMakeLists.txt
   |-\subdir_A1
   |  |-CMakeLists.txt
   |  |-a1.cpp
   |
   |-\subdir_A2
      |-CMakeLists.txt
      |-a2.cpp

Project B
|-\lib
   |-a1.h
   |-a2.h
   |-lib_ProjectA.a
|-\src
   |-CMakeLists.txt
   |-b.cpp

The problem is that project B can't resolve the project's A definitions. Although I've added target_link_libraries to CMakeLists.txt in Project B, I have an error like this:

undefined reference to `project_a::a1::func1()"

UPD1

I managed to compile Project B by copping all libraries from subdirectories (liba1.a, liba2.a) and linking them to the project. I wonder if it's possible to tune Project A, so that I can do with only one file lib_ProjectA.a

UPD2

Code:

Project A

add_library (adapter
   adapter.cpp
 )

target_link_libraries (adapter PUBLIC
   net  # From project's A subdirectory 
   utils # From project's A subdirectory 
)

Project B

add_library (anthill
   functional_block.cpp)

target_link_libraries(anthill 
  ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a
  ${PROJECT_SOURCE_DIR}/lib/libadapter.a 
  ${PROJECT_SOURCE_DIR}/lib/libnet.a     # Can't compile without it   
  ${PROJECT_SOURCE_DIR}/lib/libutils.a   # Can't compile without it
)

使用OBJECT选项的最佳解决方案:

 add_library(myObjects OBJECT a.c b.c)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM