0

I am confused on the right way to get an external library integrated into my own Cmake project (This external project needs to be built along with my project, it's not installed separately, so we can't use find_library, or so I think)

Let's assume we have a project structure like this (simplified for this post):

my_proj/
--CMakeLists.txt
--src/
  +---CMakeLists.txt
  +---my_server.cpp

That is, we have a master CMakeLists.txt that basically sits at root and invokes CMakeLists for sub directories. Obviously, in this example, because its simplified, I'm not showing all the other files/directories.

I now want to include another C++ GitHub project in my build, which happens to be this C++ bycrypt implementation: https://github.com/trusch/libbcrypt

My goal:

  • While building my_server.cpp via its make process, I'd like to include the header files for bcrypt and link with its library.

What I've done so far: - I added a git module for this external library at my project root:

[submodule "third_party/bcrypt"]
        path = third_party/bcrypt
        url = https://github.com/trusch/libbcrypt

So now, when I checkout my project and do a submodule update, it pulls down bcrypt to ${PROJ_ROOT}/third_party

Next up, I added this to my ROOT CMakeLists.txt

# Process subdirectories
add_subdirectory(third_party/bcrypt) 
add_subdirectory(src/)

Great. I know see when I invoke cmake from root, it builds bcrypt inside third_party. And then it builds my src/ directory. The reason I do this is I assume this is the best way to make sure the bcrypt library is ready before my src directory is built.

Questions:

a) Now how do I correctly get the include header path and the library location of this built library into the CMakeLists.txt file inside src/ ? Should I be hardcoding #include "../third_party/bcrypt/include/bcrypt/bcrypt.h" into my_server.cpp and -L ../third_party/libcrypt.so into src/CMakeLists.txt or is there a better way? This is what I've done today and it works, but it looks odd

I have, in src/CMakeLists.txt

set(BCRYPT_LIB,"../third_party/bcrypt/libbcrypt.so")
target_link_libraries(my app ${MY_OTHERLIBS} ${BCRYPT_LIB})

b) Is my approach of relying on sequence of add_directory correct?

Thank you.

user1361529
  • 2,667
  • 29
  • 61

1 Answers1

0

The best approach depends on what the bcrypt CMake files are providing you, but it sounds like you want to use find_package, rather than hard-coding the paths. Check out this answer, but there are a few different configurations for find_package: MODULE and CONFIG mode.

If bcrypt builds, and one of the following files gets created for you:

FindBcrypt.cmake
bcrypt-config.cmake
BcryptConfig.cmake

that might give you an idea for which find_package configuration to use. I suggest you check out the documentation for find_package, and look closely at how the search procedure is set up to determine how CMake is searching for bcrypt.

Kevin
  • 16,549
  • 8
  • 60
  • 74