13

I want to build the static library on a bin subdir, but cmake seems not not doing what I want. Here is the Cmake file I wrote.

cmake_minimum_required(VERSION 3.3)
project(FancyLogger)

set(SOURCE_FILES FancyLogger.cpp)

set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

add_library(FancyLogger STATIC ${SOURCE_FILES})

I made a build subdir, and ran cmake .. and make in this sub directory, hoping that the output static library will be generated on the bin library.

But the output remains in the build directory, Why?

Here is my file tree

===== |

| FancyLogger.cpp

| CMakeLists.txt

| /build

| /bin

richard.g
  • 3,585
  • 4
  • 16
  • 26

1 Answers1

26

For static libraries you have to set the CMAKE_ARCHIVE_OUTPUT_DIRECTORY, not the CMAKE_LIBRARY_OUTPUT_DIRECTORY.

So you will have:

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)

See how do I make cmake output into a 'bin' dir?

By the way, it's probably not a good idea to create subfolders within the source directory, especially in an automated way.

Community
  • 1
  • 1
Antonio
  • 19,451
  • 13
  • 99
  • 197
  • 3
    I agree. You should use ${CMAKE_CURRENT_BINARY_DIR}/bin or ${CMAKE_BINARY_DIR}/bin instead – Dimitri Merejkowsky Mar 18 '16 at 23:20
  • 1
    @DimitriMerejkowsky why is that not a good idea. Your saying the following is bad? set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib). – Dblock247 Jun 10 '18 at 04:01
  • Yes. For instance, if you want to compile for linux and cross-compile for android, you can have a `build/linux` and a `build/android` folder, and thus your library should be in `bulid/linux/lib` or `build/android/lib` so you don't mix binaries built for different operating systems in the same directory. – Dimitri Merejkowsky Jun 11 '18 at 09:55