CMake adds unnecessary dependencies between .o and .a files

后端 未结 1 1628
悲&欢浪女
悲&欢浪女 2020-12-18 04:32

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 o

相关标签:
1条回答
  • 2020-12-18 05:13

    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 讨论(0)
提交回复
热议问题