Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c++ - How to Link a static external lib using cmake

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 FilesCMakeincmake.exe" --build E:Workspaceflecs-projectcmake-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 Studio2019CommunityVCToolsMSVC14.27.29110inHostX64x64
make.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.27.29110inHostX64x64
make.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.27.29110inHostX64x64
make.exe"' : return code '0x2'
Stop.

EDIT 1:

Original linker output:

LINK: command "C:PROGRA~2MICROS~12019COMMUN~1VCToolsMSVC1427~1.291inHostx64x64link.exe /nologo @CMakeFilesflecs_project.dirobjects1.rsp /out:flecs_project.exe /implib:flecs_project.lib /pdb:E:Workspaceflecs-projectcmake-build-releaseflecs_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 FilesCMakeincmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.27.29110inHostX64x64
make.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.27.29110inHostX64x64
make.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:Program Files (x86)Microsoft Visual Studio2019CommunityVCToolsMSVC14.27.29110inHostX64x64
make.exe"' : return code '0x2'
Stop.
question from:https://stackoverflow.com/questions/65920016/how-to-link-a-static-external-lib-using-cmake

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...