Search code examples
c++cmakeconan

Passing a c library path from a conan package to a source file


I'm using Conan to package an old C library.

The library has a loading process that requires you provide the path to a library from inside a .cpp source file. How can the directory to the conan packaged lib be accessed from inside the consuming source?

So the setup is like:

conan package roughly like:

ole_c/0.0:
    
    |--include
    
       |-- ole_c_api.h
    
    |--bin
    
       |-- libole_c.so

with a consuming: consumer_main.cpp

Then consumer_main.cpp includes ole_c_api.h and has to call a function passing the path to libole_c.so.

How can you pass that path to consumer_main.cpp? One possibility is to pass a compile flag -DPATH_TO_OLE_C_LIB= which can then get stringified by a macro inside consumer_main.cpp. How can you access the path to libole_c.so from either inside cmake or in the ole_c packaging conan.py and pass it appropriately?


Solution

  • You can use cmake generator expressions to get the path to a target's library. If you are using conan in TARGETS mode then you could do:

    target_compile_definitions(MyApp PRIVATE PATH_TO_OLE_C_LIB=$<TARGET_FILE:CONAN_PKG::ole_c>)
    

    Alternatively you might be able to populate the cpp-info.defines in the conan recipe and make use of the package_folder property to get the installed path of the package (I'm not sure how well this will work).