1

On windows cmake build the libraries in

${CMAKE_BINARY_DIR}/<< Libname >>/<< Debug or Release >>/Libname.lib

on ubuntu-mate in

${CMAKE_BINARY_DIR}/Libname.a

is there a way to get this pathes? My use-case for such a FULL_PATH_TO_LIB is as follows:

add_library(${LIB_NAME} STATIC ${SOURCES})
#...
target_link_libraries(${PROJECT_BENCH_NAME} 
 debug ${FULL_PATH_TO_LIB}${LIB_NAME}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX })
user1235183
  • 3,002
  • 1
  • 27
  • 66
  • Just link using **library target**: `target_link_libraries(${PROJECT_BENCH_NAME} debug ${LIB_NAME})`. – Tsyvarev May 18 '16 at 13:50
  • i want full path to avoid linking against wrong version – user1235183 May 18 '16 at 13:55
  • That version should be the only one in CMAKE_BINARY_DIR (unless you are compiling multiple versions from the same CMakeLists.txt, at which point they should be separate `add_library()` targets, which again makes any reference to them unambiguous.) CMake does so many nice things automatically for you (like the build type postfixing), why second-guess them? – DevSolar May 18 '16 at 14:35
  • Linking by *target name* provides linking to correct library. This is **NOT** a linking by a *library name*. But you may also use [generator expression](https://cmake.org/cmake/help/v3.0/manual/cmake-generator-expressions.7.html): `target_link_libraries(${PROJECT_BENCH_NAME} debug $)`. – Tsyvarev May 18 '16 at 14:36
  • @Tsyvarev Actually this is a problem I actually encountered because of some ambiguity with .lib, .lib.a and .dll files I was getting with cmake + mingw under Windows. I solved that by defining a custom add_dependencies macro, and I did need to generate the full filename. – Antonio May 20 '16 at 11:51
  • @Antonio: I do not argue against usefulness of full library path at all. I just say that in given case, when linking with library creating within a project, target name is sufficient. – Tsyvarev May 20 '16 at 11:56
  • @Tsyvarev I agree with you – Antonio May 20 '16 at 12:00

1 Answers1

0

You can customize the output directory, separately for executables, archive libraries (.a/.lib) and dynamic libraries (.dll/.so), and then use this information to link to a specific file. Here you find a detailed explanation. (You can also set the output directory target-wise)

For example, in your case you would set CMAKE_ARCHIVE_OUTPUT_DIRECTORY:

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib/)
add_library(${LIB_NAME} STATIC ${SOURCES})
#...
target_link_libraries(${PROJECT_BENCH_NAME} 
 debug ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}${LIB_NAME}${CMAKE_DEBUG_POSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX })
Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197