0

Here is the project tree : It's a static library that uses another static library

Static_lib/
    |
    ----src/
        |----src1.cpp
        |----src2.cpp
    ----inc/
        |----head1.h
        |----head2.h
    ----Static_lib2/
        |----lib/
            |----32/
                |Debug/
                    lib2.lib
                |Release/
                    lib2.lib
            |----64/
                |Debug/
                    lib2.lib
                |Release/
                    lib2.lib
        |----include/
            |bunch of headers
    ----build/
    ----CMakeLists.txt

Here is my CMakeLists.txt :

cmake_minimum_required(VERSION 3.5.1)
project(Static_lib)

set(SOURCE_FILES src/src1.cpp src/src2.cpp)
set(HEADER_FILES inc/head1.h inc/head2.h)

add_library(Static_lib STATIC ${SOURCE_FILES} ${HEADER_FILES})

target_include_directories(Static_lib PUBLIC 
    ${CMAKE_SOURCE_DIR}/inc 
    ${CMAKE_SOURCE_DIR}/Static_lib2/include
)

if(CMAKE_SYSTEM_NAME STREQUAL Linux)

    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS FALSE)
    else(CMAKE_SIZEOF_VOID_P EQUAL 8)
        set_property(GLOBAL PROPERTY FIND_LIBRARY_USE_LIB32_PATHS TRUE)
    endif(CMAKE_SIZEOF_VOID_P EQUAL 8)

    find_library(STATIC_LIB2 libcrypto.a)
    target_link_libraries(Static_lib "${STATIC_LIB2}")

elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)

    if(CMAKE_SIZEOF_VOID_P EQUAL 8)
        target_link_libraries(Static_lib debug ${CMAKE_SOURCE_DIR}/Static_lib2/lib/64/Debug/lib2.lib)

        target_link_libraries(Static_lib optimized ${CMAKE_SOURCE_DIR}/Static_lib2/lib/64/Release/lib2.lib)

    else(CMAKE_SIZEOF_VOID_P EQUAL 8)
        target_link_libraries(Static_lib debug ${CMAKE_SOURCE_DIR}/Static_lib2/lib/32/Debug/lib2.lib)

        target_link_libraries(Static_lib optimized ${CMAKE_SOURCE_DIR}/Static_lib2/lib/32/Release/lib2.lib)

    endif(CMAKE_SIZEOF_VOID_P EQUAL 8)

endif(CMAKE_SYSTEM_NAME STREQUAL Linux)

When I create the project using Visual studio generator, the project is created with no error or warning, however there are no dependencies and no libraries in the project properties.

After building, I get a library that doesn't actually have the size it is supposed to have.

Can someone give some insight? Maybe my CMakeLists code is wrong?

Thanks in advance!

  • First thing when debug missed linked - make sure, that corresponded **code is actually executed**. As all `target_link_libraries` calls are under *conditional* branches, check that appropriate branch is actually processed. E.g., you may add `message()` calls into it. – Tsyvarev Apr 28 '17 at 21:04
  • Thank you for answer. I had a couple of mistakes, like misusing NOT operator. I used MESSAGE and the correspending code is actually now executed. However, even after correcting all the mistakes, MSVC still doesn't link the appropriate libraries. – El Mostafa IDRASSI Apr 29 '17 at 14:17

1 Answers1

0

I just found the answer here cmake: include library dependencies in static lib

By default, static libraries are not combined together. Doing it manually is the way to go.

Community
  • 1
  • 1