Charm Young
Charm Young

Reputation: 1

cmake how to avoid building lib everytime

file structure

├── a_lib
│   ├── a.cpp
│   └── a.h
├── CMakeLists.txt
└── main.cpp

CmakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(untitled)

set(CMAKE_CXX_STANDARD 11)

add_library(a_lib SHARED a_lib/a.cpp)
target_include_directories(a_lib PUBLIC a_lib/)

add_executable(untitled main.cpp)
target_link_libraries(untitled a_lib)

But every time I build the a_lib will be rebuilt when a_lib is not changed.

cmake --build /home/autocar/Workspace/untitled/cmake-build-debug --target untitled -- -j 4
[ 50%] Built target a_lib
[100%] Built target untitled

So what should I do, if I do not want to rebuild the linked library every time. It takes a long time when a_lib builds every time even without any changes.

Upvotes: 0

Views: 1156

Answers (2)

gordan.sikic
gordan.sikic

Reputation: 1640

Just one addition to arrowd's answer:

If you want to completely avoid check if any target is up to date, you can issue the following command:

cmake --build /home/autocar/Workspace/untitled/cmake-build-debug --target untitled/fast -- -j 4

Note that --target untitled is changed into --target untitled/fast.

You can add "/fast" to any target mentioned in CMakeLists.txt to trigger compilation without any additional checks. On the other hand, be extremely cautious, since this allows you create inconsistent results, in case when sources for a_lib are changed, and target a_lib should be compiled/linked as well.

Upvotes: 3

arrowd
arrowd

Reputation: 34401

Judging from this output

cmake --build /home/autocar/Workspace/untitled/cmake-build-debug --target untitled -- -j 4
[ 50%] Built target a_lib

nothing's actually gets rebuilt. CMake just checks that this target is up to date.

The compilation takes place when CMake prints Building CXX object a.cpp.o and linking is performed when Linking CXX library a_lib.so pops out. If you don't see these messages, the library doesn't get rebuilt.

Upvotes: 0

Related Questions