Reputation: 45
I have a project where I mix between static and shared libs. The shared libs are delivered. Each shared lib is built with its correspondence static lib. A shared lib can depend on other shared libs as well. For example, I have: sharedA
, sharedB
, sharedC
, staticA
, staticB
and staticC
. SharedA
is built with staticA
inside and similar for other shared. Let's say sharedC
, besides its dependency on staticC
, also depends on sharedB
.
For the static libraries, if they need dependency on another static one, I link against the one's correspondence shared lib. For example:
staticB
depends on staticA
. Hence, I link staticB
against sharedA
. The reason for this is to avoid size increase if statically declare staticB's dependency on staticA
.
This system has been working. However, problem arises when I build the static libs with hidden visibility. The linker links with no error. But more than one symbols of a specific class might end up in the final symbol map. Therefore, the program can pick up the undesired one, which causes weird behaviour.
I have been reading about cmake object library, and probably switching the static libs to object libs could help in this case. I change the static libs to object, and get undefined symbol errors. I define dependency of an object lib on other, but still could not get rid of all the errors. My questions are:
target_link_libraries(obj2 PUBLIC obj)
to declare dependency between 2 object libs. Can this be the reason for undefined symbol error I get? Because according to cmake documentation:Object Libraries may "link" to other object libraries to get usage requirements, but since they do not have a link step nothing is done with their object files.
Maybe I should switch to target_link_libraries(obj2 $<TARGET_OBJECTS:obj>)
so obj2
is built with object files from obj.c
? Another thing with this is: obj
needs to be a library (created with add_library
). In the function where I define all the links, obj
is a variable and could not be put after $<TARGET_OBJECTS:
. I have been looking around but not able to find anything. Is there a way I can put variable name after $<TARGET_OBJECTS:
, or I have to refractor somehow to make this possible?
Any other suggestion on how to do this would be helpful to me
Upvotes: 1
Views: 151