user2346536
user2346536

Reputation: 1474

Force CMAKE to use given library and nothing else

Position of the problem

I'm trying to force cmake to use one particular library. I want the executable to be linked statically with this one and no other. I want the build to fail if it can't link both "libgpg-error.a" and "libgcrypt.a" statically from the "/XXX/static_libs" directory.

What I have so far:

So far, my CMakeLists.txt looks like this:

# Project Setup
project(file_crypt)
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})

# Includes
include_directories(.)

# Link
link_directories( ${gcrypt_lib_dir} ${dl_crypt_lib_dir} )
find_library( gcrypt_libs NAMES libgcrypt.a libgpg-error.a PATHS ${gcrypt_lib_dir} NO_DEFAULT_PATH )

# Executables Declarations
add_executable( my_decrypt
                my_decrypt.cpp [...] )
set_target_properties( my_decrypt PROPERTIES COMPILE_FLAGS "-m32" LINK_FLAGS "-m32" )


# Link to libraries
target_link_libraries( my_decrypt ${gcrypt_libs} dl crypt )

I added:

gcrypt_lib_dir:FILEPATH=/XXX/static_libs

to my CMakeCache.txt. And if I do "ls" on "/XXX/static_libs", I can see both "libgpg-error.a" and "libgcrypt.a".

Then I run cmake. Then gcrypt_libs variable is set to not found

Questions

  1. Why would cmake not find those libs ?
  2. is The above file correct ?

Upvotes: 1

Views: 857

Answers (1)

atsui
atsui

Reputation: 1008

I think you should set gcrypt_libs in your CMakeLists.txt rather than in CMakeCache.txt, which is an autogenerated file. I believe if you use the set command to set the variable, it should work. Add a line like:

set( gcrypt_libs /XXX/static_libs )

right before your call to find_library and it should work.

Update: As mentioned in your comment, /XXX/static_libs is a machine-dependent location and shouldn't be hardcoded into CMakeLists.txt. Your idea to initialize variables with cmake -C <initial-cache> should work perfectly -- the only thing is, as cmake's manpage points out: "The given file should be a CMake script containing SET commands that use the CACHE option, not a cache-format file."

So say you keep an initial cache file called paths.txt, it could have

set( gcrypt_libs /XXX/static_libs CACHE FILEPATH "" )

Then you can configure with cmake in the usual way:

cmake -C path/to/paths.txt path/to/source

So basically, you still don't touch CMakeCache.txt but write a script to load values into the cache like so.

Upvotes: 1

Related Questions