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

1
  • The practice is to install static libs in the lib dir, not the bin dir. Commented Feb 10, 2023 at 1:19

1 Answer 1

27

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I agree. You should use ${CMAKE_CURRENT_BINARY_DIR}/bin or ${CMAKE_BINARY_DIR}/bin instead
@DimitriMerejkowsky why is that not a good idea. Your saying the following is bad? set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib).
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.