How to Link a static external lib using cmake

0

I am currently trying to build a conan package. Part of that is to prebuild a static library using cmake(repo). At the moment I can not figure out how to link the generated static lib inside another build.

  1. The library I am building is flecs
  2. Using flecs inside a cmake sub directory works fine
  3. Using the generated lib seems to be not enough for cmake to be able to link

working sample using cmake sub directory:

cmake_minimum_required(VERSION 2.8.7)

project(flecs_project)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)

add_subdirectory(flecs)

add_executable(flecs_project)
target_sources(flecs_project PRIVATE
        helloworld/include/helloworld.h
        helloworld/src/main.cpp)

target_include_directories(flecs_project PRIVATE flecs/include)
target_include_directories(flecs_project PRIVATE helloworld/include)

target_link_libraries(flecs_project PRIVATE flecs_static)

not working sample using lib

cmake_minimum_required(VERSION 2.8.7)

project(flecs_project)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_C_STANDARD 99)

add_executable(flecs_project)
target_sources(flecs_project PRIVATE
        helloworld/include/helloworld.h
        helloworld/src/main.cpp)

target_include_directories(flecs_project PRIVATE flecs/include)
target_include_directories(flecs_project PRIVATE helloworld/include)

target_link_libraries(flecs_project "E:\\Workspace\\flecs-project\\cmake-build-release\\flecs\\flecs_static.lib")

I am sure there is something I have not understood about static linkage, but I can not find any resources about what I am missing here.

Thanks for everyone who tries to help!

Edit 2:

As @Tsyvarev pointed out the header I was using needed to have the flecs_STATIC - flag set to be used for static linkage.

Now I get the following make output

[ I will try to fix that myself. But if you have any suggestions I would appreciate them. :-) ]

"C:\Program Files\CMake\bin\cmake.exe" --build E:\Workspace\flecs-project\cmake-build-release --target flecs_project
Scanning dependencies of target flecs_project
[ 50%] Building CXX object CMakeFiles/flecs_project.dir/helloworld/src/main.cpp.obj
main.cpp
NMAKE : fatal error U1073: don't know how to make '..\flecs_static.lib'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.

EDIT 1:

Original linker output:

LINK: command "C:\PROGRA~2\MICROS~1\2019\COMMUN~1\VC\Tools\MSVC\1427~1.291\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\flecs_project.dir\objects1.rsp /out:flecs_project.exe /implib:flecs_project.lib /pdb:E:\Workspace\flecs-project\cmake-build-release\flecs_project.pdb /version:0.0 /machine:x64 /INCREMENTAL:NO /subsystem:console ..\flecs_static.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:flecs_project.exe.manifest" failed (exit code 1120) with the following output:
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_new_component referenced in function "class flecs::entity __cdecl flecs::pod_component<struct EcsComponent>(class flecs::world const &,char const *,bool)" (??$pod_component@UEcsComponent@@@flecs@@YA?AVentity@0@AEBVworld@0@PEBD_N@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_component_has_actions referenced in function "void __cdecl flecs::_::register_lifecycle_actions<struct Position>(struct ecs_world_t *,unsigned __int64,bool,bool,bool,bool)" (??$register_lifecycle_actions@UPosition@@@_@flecs@@YAXPEAUecs_world_t@@_K_N222@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_init_w_args referenced in function "public: __cdecl flecs::world::world(int,char * * const)" (??0world@flecs@@QEAA@HQEAPEAD@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_fini referenced in function "public: __cdecl flecs::world::~world(void)" (??1world@flecs@@QEAA@XZ)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_set_component_actions_w_entity referenced in function "void __cdecl flecs::_::register_lifecycle_actions<struct Position>(struct ecs_world_t *,unsigned __int64,bool,bool,bool,bool)" (??$register_lifecycle_actions@UPosition@@@_@flecs@@YAXPEAUecs_world_t@@_K_N222@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_new_component_id referenced in function "public: __cdecl flecs::entity::entity(class flecs::world const &,char const *,bool)" (??0entity@flecs@@QEAA@AEBVworld@1@PEBD_N@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_get_w_entity referenced in function "public: struct Position const * __cdecl flecs::entity::get<struct Position>(void)const " (??$get@UPosition@@@entity@flecs@@QEBAPEBUPosition@@XZ)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_set_ptr_w_entity referenced in function "public: void __cdecl flecs::entity::invoke<class <lambda_4741e5ecf4b2316809d844be84aba778> >(class <lambda_4741e5ecf4b2316809d844be84aba778> &&)const " (??$invoke@V<lambda_4741e5ecf4b2316809d844be84aba778>@@@entity@flecs@@QEBAX$$QEAV<lambda_4741e5ecf4b2316809d844be84aba778>@@@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_get_type referenced in function "class flecs::entity __cdecl flecs::pod_component<struct EcsComponent>(class flecs::world const &,char const *,bool)" (??$pod_component@UEcsComponent@@@flecs@@YA?AVentity@0@AEBVworld@0@PEBD_N@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_lookup_path_w_sep referenced in function "public: class flecs::entity __cdecl flecs::world::entity<char const (&)[9]>(char const (&)[9])const " (??$entity@AEAY08$$CBD@world@flecs@@QEBA?AVentity@1@AEAY08$$CBD@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_get_path_w_sep referenced in function "class flecs::entity __cdecl flecs::pod_component<struct EcsComponent>(class flecs::world const &,char const *,bool)" (??$pod_component@UEcsComponent@@@flecs@@YA?AVentity@0@AEBVworld@0@PEBD_N@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_add_path_w_sep referenced in function "public: class flecs::entity __cdecl flecs::world::entity<char const (&)[9]>(char const (&)[9])const " (??$entity@AEAY08$$CBD@world@flecs@@QEBA?AVentity@1@AEAY08$$CBD@Z)
main.cpp.obj : error LNK2019: unresolved external symbol __imp_ecs_os_api referenced in function "class flecs::entity __cdecl flecs::pod_component<struct EcsComponent>(class flecs::world const &,char const *,bool)" (??$pod_component@UEcsComponent@@@flecs@@YA?AVentity@0@AEBVworld@0@PEBD_N@Z)
flecs_project.exe : fatal error LNK1120: 13 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files\CMake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\HostX64\x64\nmake.exe"' : return code '0x2'
Stop.
c++
cmake
static-linking
lib
conan
asked on Stack Overflow Jan 27, 2021 by Robert Andreas Fritsch • edited Jan 27, 2021 by Robert Andreas Fritsch

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0