0

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
)
Cœur
  • 37,241
  • 25
  • 195
  • 267
atimin
  • 499
  • 4
  • 11
  • Maybe setting `link_directories(${CMAKE_SOURCE_DIR}/lib)` in your `CMakeLists.txt` in project `B` is enough. Not sure about that, have a try and I'll add it as an answer if it solves. – skypjack Apr 19 '16 at 05:44
  • Can you please show us the relevant parts of your CMake files? – MikeMB Apr 19 '16 at 07:01
  • @MikeMB Actually the project is much bigger and has a lot of subdirectories with other names. I'm afraid I'll make it only more confusing. Now the problem is to compile project B without copping liba1.a and liba2.a. Is it possible? – atimin Apr 19 '16 at 07:38
  • `undefined reference` with command `target_link_libraries` means that library *has been found*, but it *doesn't define* required symbol. Something **wrong with your code**, but you don't show it. What do you expect from us? – Tsyvarev Apr 19 '16 at 07:49
  • @Tsyvarev the question isn't about my code, it's about opportunity to compile a project split into some subdirectories as a solid static library. Please, look at the update. – atimin Apr 19 '16 at 07:58
  • Thats what [mcves](/help/mcve) are for. If your project is too complex to strip it down, then create a new dummy project whith the structure in the question and try to reproduce the error there. – MikeMB Apr 19 '16 at 08:03
  • You may directly link to libraries within `project A`, no needs to copy them somewhere. In any case, your problem is still unclear for me. Can you provide code(simplified) which works for you, and code which you want to work? – Tsyvarev Apr 19 '16 at 08:03
  • Sorry, my mistake: Do I understand you right, that you don't build project A as part of project B? – MikeMB Apr 19 '16 at 08:15
  • @MikeMB Yes, you do. At first I build prorject A then copy needed files to project B. It might be useful to see the passages from cmake files in the question's updates. – atimin Apr 19 '16 at 08:21
  • In that case you need to create a1 and a2 as [object libraries](https://cmake.org/Wiki/CMake/Tutorials/Object_Library) – MikeMB Apr 19 '16 at 09:09
  • @flipback For some details on "use CMake-enabled libraries in your CMake project" you may want to look at [Making cmake library accessible by other cmake packages automatically](http://stackoverflow.com/questions/33462209/making-cmake-library-accessible-by-other-cmake-packages-automatically) – Florian Apr 19 '16 at 13:58
  • @MikeMB This is that I needed. Could you make an answer? I'd like to close the question. – atimin Apr 19 '16 at 15:10

1 Answers1

0

The best solution to use OBJECT option:

 add_library(myObjects OBJECT a.c b.c)
atimin
  • 499
  • 4
  • 11