Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using cmake, how to link an object file built by an external_project statement into another library?

Tags:

cmake

In our project, we want to use a third-party library (A) which is built using autotools and which generates an object file (B) which we need @ link time of one of our libraries (C).

external_project(
    A
    ...
)
set_source_files_properties(B PROPERTIES DEPEND A)
add_library(C ... A)
add_dependency(C B)

I had the impression that this should do the trick, but the cmake command fails by stating that it cannot find file A during the check for add_library.

Any fixes or alternative solutions would be greatly appreciated! (changing the third-party library is not an option) thanks!

like image 717
Broes De Cat Avatar asked Oct 22 '22 14:10

Broes De Cat


1 Answers

There are a few issues here:

  • external_project should be ExternalProject_Add
  • Source files have no property called DEPEND available - the set_source_files_properties command has no effect here. (Here are the properties available on source files)
  • add_library expects a list of source files to be passed, not another CMake target (which A is)
  • add_dependency should be add_dependencies

Apart from those 4 lines it's all OK :-)

So the issue is going to be that you want to include the object file B in the add_library call, but it's not going to be available at configure-time (when CMake is invoked), only at build time.

I think you're going to have to do something like:

ExternalProject_Add(
    A
    ...
)

set_source_files_properties(
    ${B} PROPERTIES
    EXTERNAL_OBJECT TRUE  # Identifies this as an object file
    GENERATED TRUE  # Avoids need for file to exist at configure-time
)

add_library(C ... ${B})
like image 194
Fraser Avatar answered Jan 23 '23 10:01

Fraser