Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7655749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:35:35+00:00 2026-05-31T12:35:35+00:00

I’ve got a project managed by CMake with multiple libraries that have link time

  • 0

I’ve got a project managed by CMake with multiple libraries that have link time dependencies between them, but each of the libraries can be compiled independently of each other. How do I express this to CMake so that I can build all the libraries concurrently?

For example, I tried this CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project (test)
add_library(lib1 STATIC lib1.cpp)
add_library(lib2 STATIC lib2.cpp)
add_library(lib3 STATIC lib3.cpp)
add_executable(main main.cpp)
target_link_libraries(lib2 lib1)
target_link_libraries(lib3 lib2)
target_link_libraries(main lib3)

Each file just defines a different empty function like:

void f1() {}

When I type cmake . && make -j4, I see this:

[ 25%] Building CXX object CMakeFiles/lib1.dir/lib1.cpp.o
Linking CXX static library liblib1.a
[ 25%] Built target lib1
[ 50%] Building CXX object CMakeFiles/lib2.dir/lib2.cpp.o
Linking CXX static library liblib2.a
[ 50%] Built target lib2
[ 75%] Building CXX object CMakeFiles/lib3.dir/lib3.cpp.o
Linking CXX static library liblib3.a
[ 75%] Built target lib3
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
[100%] Built target main

Even though I’ve specified -j4 and compiling each .cpp file should never depend on any .a files, it’s waiting on the previous compile and link to finish begin the next one. I’d rather see something like:

Building CXX object CMakeFiles/lib1.dir/lib1.cpp.o
Building CXX object CMakeFiles/lib2.dir/lib2.cpp.o
Building CXX object CMakeFiles/lib3.dir/lib3.cpp.o
Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX static library liblib1.a
Built target lib1
Linking CXX static library liblib2.a
Built target lib2
Linking CXX static library liblib3.a
Built target lib3
Linking CXX executable main
Built target main

Is it possible to tell CMake that it can build all the .o files concurrently?

In reality, I’m doing this in a million-line project with about 20 CPU cores at my disposal (with distcc), so this is a huge bottleneck on my build times.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:35:36+00:00Added an answer on May 31, 2026 at 12:35 pm

    The sequential execution is probably a consequence of the link dependencies established between the static libraries lib1, lib2 and lib3.

    One work-around is to get rid of these static library link dependencies. Since you are building static libraries anyway, removing the dependencies will not prevent them from being linked successfully. The executable main needs to depend on all libraries then:

    cmake_minimum_required(VERSION 2.6)
    project (test)
    add_library(lib1 STATIC lib1.cpp)
    add_library(lib2 STATIC lib2.cpp)
    add_library(lib3 STATIC lib3.cpp)
    add_executable(main main.cpp)
    target_link_libraries(main lib1 lib2 lib3)
    

    Organized this way make -j builds the libraries in parallel.

    If getting rid of the link dependencies is not an option, you can apply the principle “Any problem in computer science can be solved with another layer of indirection”:

    cmake_minimum_required(VERSION 2.6)
    project (test)
    add_library(lib1_objects STATIC lib1.cpp)
    add_library(lib2_objects STATIC lib2.cpp)
    add_library(lib3_objects STATIC lib3.cpp)
    add_executable(main main.cpp)
    
    add_library(lib1 STATIC empty.cpp)
    add_library(lib2 STATIC empty.cpp)
    add_library(lib3 STATIC empty.cpp)
    
    target_link_libraries(lib1 lib1_objects)
    target_link_libraries(lib2 lib2_objects lib1)
    target_link_libraries(lib3 lib3_objects lib2)
    target_link_libraries(main lib3)
    

    This sets up helper libraries (e.g., lib1_objects), which have no dependencies and can thus be built in parallel. The original libraries link to these helper libraries and also have the required link dependencies set up. empty.cpp is just an empty dummy CPP source file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.