35

In CMake, I can't seem to output my library in ../out/library, only library. When I do the ../out/library path, it tells me it can't find the library, as if it wants to link to it.

add_library(../out/JE3D ../source/CDeviceLayerSDL.cpp)

There's more files, I'm just saving space. When I do that, I get this error.

Linking CXX static library lib../out/JE3D.a /usr/bin/ar: lib../out/JE3D.a: No such file or directory make[2]: * [lib../out/JE3D.a] Error 1 make[1]: * [CMakeFiles/../out/JE3D.dir/all] Error 2 make: *** [all] Error 2

Jookia
  • 6,544
  • 13
  • 50
  • 60

2 Answers2

56

The LIBRARY_OUTPUT_DIRECTORY target property specifies the directory where library target files will be built.

set_target_properties(JE3D PROPERTIES
         LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/library)

If all the libraries are in one directory, I find it more convenient to set the CMAKE_LIBRARY_OUTPUT_DIRECTORY variable, which is used to initialize the LIBRARY_OUTPUT_DIRECTORY property when creating a target.

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/library)

Edit: Check comments if your target is a static library

Antonio
  • 19,451
  • 13
  • 99
  • 197
Chin Huang
  • 12,912
  • 4
  • 46
  • 47
  • 2
    The reason you can't just use path components in the library name is because CMake is simplistically mangling that name to get the library name. On your host, it's using the pattern `lib${FILE}.a`, which doesn't work when you try to use a path. – Jack Kelly Sep 19 '10 at 13:00
  • 44
    LIBRARY_OUTPUT_DIRECTORY doesn't work for me. According to documentation this property controls output directory for shared libraries. Static libraries are controlled by ARCHIVE_OUTPUT_DIRECTORY property. – Jarlaxle Nov 17 '12 at 21:25
  • 7
    Even ARCHIVE_OUTPUT_DIRECTORY doesn't work for me, whatever I put as last argument, the lib is always put in the /lib no matter what I specify! set_target_properties(libdocopt PROPERTIES IMPORTED_LOCATION ${PROJECT_BINARY_DIR}/out/library}} Even worse, on MacOS if I specify /lib/libdocopt.a it works. On Linux it is placed in /lib64/libdocopt.a :-O And yes, I did include(GNUInstallDirs). Does anybody has a clue? – jaques-sam May 31 '18 at 19:46
  • It doesn't work for object library – quent Jan 12 '22 at 11:13
3

My 2 cents, I was trying to change the output directory of a static library (.lib) in Visual Studio. Only thing I found that worked for me was:

set_target_propertes(${PROJECT_NAME} PROPERTIES
    ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/myPath"
)

This made the output path for the library

${CMAKE_BINARY_DIR}/myPath/Debug

I assume building a Release configuration would change that path accordingly. But I had to use set_target_properties, trying to do

set(ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/myPath")

didn't work. This is for CMake version 3.19.5.

yano
  • 4,827
  • 2
  • 23
  • 35