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

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.
The library I am building is flecs
Using flecs inside a cmake sub directory works fine
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.

Related

build poco c++ library on windows

I'm trying to build poco library (c++) with openssl on windows 10 (VS 2012). With the following cmd file:
#echo off
set INCLUDE=c:\openssl\include
set LIB=c:\openssl\lib
set _IsNativeEnvironment=true
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat" amd64
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin\amd64\vcvars64.bat"
buildwin 110 build all release x64 nosamples notests devenv
I get link errors:
1>------ Build started: Project: Crypto, Configuration: release_shared x64 ------
1> Datei *.dll nicht gefunden
1> Datei *.lib nicht gefunden
1> Creating library ..\lib64\PocoCrypto.lib and object ..\lib64\PocoCrypto.exp
1>ECKey.obj : error LNK2001: unresolved external symbol EVP_PKEY_type
1>ECKeyImpl.obj : error LNK2001: unresolved external symbol EVP_PKEY_type
1>RSAKeyImpl.obj : error LNK2001: unresolved external symbol EVP_PKEY_type
1>PKCS12Container.obj : error LNK2001: unresolved external symbol EVP_PKEY_type
1>EVPPKey.obj : error LNK2019: unresolved external symbol EVP_PKEY_type referenced in function "private: void __cdecl Poco::Crypto::EVPPKey::newECKey(char const *)" (?newECKey#EVPPKey#Crypto#Poco##AEAAXPEBD#Z)
...
(Datei *.dll nicht gefunden == file *.dll not found).
If I remove the Crypto in file "components" (Is it OK if I want to use https?), I get the following error:
1>------ Build started: Project: NetSSL_OpenSSL, Configuration: release_shared x64 ------
1> Creating library ..\lib64\PocoNetSSL.lib and object ..\lib64\PocoNetSSL.exp
1>Context.obj : error LNK2019: unresolved external symbol BIO_new_file referenced in function "private: void __cdecl Poco::Net::Context::initDH(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?initDH#Context#Net#Poco##AEAAXAEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
...
I installed openssl from "Shining Light" (win64). I also tried to use openssl which is built from source (https://github.com/pocoproject/openssl). But I get the same errors.
Can someone help me ?!
Thank you!
Xinyu
OK, If I delete all object files: folder obj64, including obj64 in testsuite, then every thing is ok.

Boost 1.55 Python with Windows Visual Studio 2013, Link Error

I download boost1.55 zip from official website and then build with command bootstrap and b2.
And I tried example code, but it has a link error.
1>LINK : fatal error LNK1104: cannot open file 'libboost_python-vc120-mt-gd-1_55.lib'
Below is what I did.
New project win32 Console application, DLL empty.
Add include path, library path in VC++ DIRECTORIES like the below.
INCLUDE DIRECTORIES: C:\boost\boost_1_55_0;C:\Python27\include
LIBRARY DIRECTORIES: C:\boost\boost_1_55_0\libs;C:\Python27\libs
.
#define BOOST_PYTHON_STATIC_LIB
#include <boost/python.hpp>
using namespace boost::python;
struct World
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<World>("World")
.def("greet", &World::greet)
.def("set", &World::set);
}
Edit
If i add the library directory, C:\boost_1_55_0\stage\liblike an answer below, it make many errors happen, around 200
below is just the part of errors.
1>------ Build started: Project: hello, Configuration: Debug Win32
------ 1> hello.cpp 1>c:\python27\include\pymath.h(22): warning C4273: 'round' : inconsistent dll linkage 1> c:\program files
(x86)\microsoft visual studio 12.0\vc\include\math.h(516) : see
previous definition of 'round' 1>c:\program files (x86)\microsoft
visual studio 12.0\vc\include\xtgmath.h(190): warning C4273: '_hypot'
: inconsistent dll linkage 1> c:\program files
(x86)\microsoft visual studio 12.0\vc\include\math.h(538) : see
previous definition of '_hypot' 1>c:\program files (x86)\microsoft
visual studio 12.0\vc\include\xtgmath.h(206): warning C4273: 'round' :
inconsistent dll linkage 1> c:\program files (x86)\microsoft
visual studio 12.0\vc\include\math.h(516) : see previous definition of
'round' 1>c:\boost_1_55_0\boost\python\opaque_pointer_converter.hpp :
warning C4819: The file contains a character that cannot be
represented in the current code page (949). Save the file in Unicode
format to prevent data loss
1>c:\boost_1_55_0\boost\python\return_opaque_pointer.hpp : warning
C4819: The file contains a character that cannot be represented in the
current code page (949). Save the file in Unicode format to prevent
data loss 1> Creating library C:\Users\User\documents\visual studio
2013\Projects\hello\Debug\hello.lib and object
C:\Users\User\documents\visual studio
2013\Projects\hello\Debug\hello.exp 1>hello.obj : error LNK2019:
unresolved external symbol __imp__PyString_FromStringAndSize
referenced in function "public: struct _object * __thiscall
boost::python::to_python_value,class std::allocator > const
&>::operator()(class std::basic_string,class std::allocator > const &)const "
(??R?$to_python_value#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###python#boost##QBEPAU_object##ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
1>libboost_python-vc120-mt-gd-1_55.lib(builtin_converters.obj) : error
LNK2001: unresolved external symbol __imp__PyString_FromStringAndSize
1>libboost_python-vc120-mt-gd-1_55.lib(str.obj) : error LNK2001:
unresolved external symbol __imp__PyString_FromStringAndSize
1>libboost_python-vc120-mt-gd-1_55.lib(function_doc_signature.obj) :
error LNK2001: unresolved external symbol
__imp__PyString_FromStringAndSize 1>libboost_python-vc120-mt-gd-1_55.lib(list.obj) : error LNK2001:
unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(tuple.obj) : error LNK2001:
unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(function_doc_signature.obj) :
error LNK2001: unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(object_protocol.obj) : error
LNK2001: unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(pickle_support.obj) : error
LNK2001: unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(dict.obj) : error LNK2001:
unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(str.obj) : error LNK2001:
unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(from_python.obj) : error
LNK2001: unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(function.obj) : error LNK2001:
unresolved external symbol __imp___Py_NoneStruct
1>libboost_python-vc120-mt-gd-1_55.lib(module.obj) : error LNK2001:
The "LIBRARY DIRECTORY" should reference C:\boost_1_55_0\stage\lib instead of C:\boost\boost_1_55_0\libs.

Selecting a Capture Device Using the System Device Enumerator

I was tring to use this link: http://msdn.microsoft.com/en-us/library/dd377566%28v=VS.85%29.aspx, to select a capturing device.
Works perfectly on Visual Studio, but throws the following error QT:
test.obj : error LNK2005: _main already defined in main.obj
test.obj : error LNK2019: unresolved external symbol __imp__CoCreateInstance#20 referenced in function "long __cdecl EnumerateDevices(struct _GUID const &,struct IEnumMoniker * *)" (?EnumerateDevices##YAJABU_GUID##PAPAUIEnumMoniker###Z)
test.obj : error LNK2019: unresolved external symbol __imp__VariantClear#4 referenced in function "void __cdecl DisplayDeviceInformation(struct IEnumMoniker *)" (?DisplayDeviceInformation##YAXPAUIEnumMoniker###Z)
test.obj : error LNK2019: unresolved external symbol __imp__VariantInit#4 referenced in function "void __cdecl DisplayDeviceInformation(struct IEnumMoniker *)" (?DisplayDeviceInformation##YAXPAUIEnumMoniker###Z)
test.obj : error LNK2019: unresolved external symbol __imp__CoUninitialize#0 referenced in function _main
test.obj : error LNK2019: unresolved external symbol __imp__CoInitializeEx#8 referenced in function _main
I'm using windows 7 & QT 5.0.2.
Any help will be appreciated thanks.
Searching for "cocreateinstance unresolved external" on google and using the first link returned:
http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/5fc032e1-86d8-43c7-870b-f10599000605/
I spotted the third comment that indicates to link against "ole32.lib".
Your reply to my comment above indicates that "oleAut32.lib" is also required.
Also make sure to link against "strmiids.lib".
To sum up, required libraries are:
ole32.lib
oleaut32.lib
strmiids.lib

LNK 2019 - Unresolved External Symbol

I am getting the below link errors.
1>------ Build started: Project: NowHook, Configuration: Debug Win32 ------
1>Compiling...
1>NowHook.CPP
1>Linking...
1>NowHook.obj : error LNK2019: unresolved external symbol __imp__VirtualProtect#16 referenced in function "void __cdecl
1>NowHook.obj : error LNK2019: unresolved external symbol __imp__GetProcAddress#8 referenced in function "void __cdecl
1>NowHook.obj : error LNK2019: unresolved external symbol __imp__LoadLibraryA#4 referenced in function "void __cdecl HookSend(void)" (?HookSend##YAXXZ)
1>C:\Users\JAGAN\Documents\Visual Studio 2008\Projects\HookNow\Debug\NowHook.dll :
fatal error LNK1120: 3 unresolved externals
1>Build log was saved at "file://c:\Users\JAGAN\Documents\Visual Studio 2008\Projects
I have included
1) Linker/General/AdditionalLibraryDirectories
2) Linker/Input/Additional Dependencies
You are missing kernel32.lib in your Linker/Input/Additional Dependencies
Make sure you are setting the Additional Dependencies for "All Configurations" and "All Platforms".

Building a static library for glut 3.7 on Windows

I've been trying to compile glut as a static library without having to link with glut32.dll on runtime. I downloaded the glut windows source code but when I try to compile (default), I keep getting:
Making in glut subdirectory...
link /INCREMENTAL:NO /NOLOGO -entry:_DllMainCRTStartup#12 -dll -out:glut32.dll -def:glut.def glut_8x13.obj glut_9x15.obj glut_bitmap.obj glut_bwidth.obj glut_cindex.obj glut_cmap.obj glut_cu
rsor.obj glut_dials.obj glut_dstr.obj glut_event.obj glut_ext.obj glut_fullscrn.obj glut_gamemode.obj glut_get.obj glut_hel10.obj glut_hel12.obj glut_hel18.obj glut_init.obj glut_input.obj glut_joy.ob
j glut_key.obj glut_keyctrl.obj glut_keyup.obj glut_mesa.obj glut_modifier.obj glut_mroman.obj glut_overlay.obj glut_roman.obj glut_shapes.obj glut_space.obj glut_stroke.obj glut_swap.obj glut_swidth.
obj glut_tablet.obj glut_teapot.obj glut_tr10.obj glut_tr24.obj glut_util.obj glut_vidresize.obj glut_warp.obj glut_win.obj glut_winmisc.obj win32_glx.obj win32_menu.obj win32_util.obj win32_winproc.o
bj win32_x11.obj opengl32.lib glu32.lib winmm.lib kernel32.lib ws2_32.lib mswsock.lib advapi32.lib user32.lib gdi32.lib comdlg32.lib winspool.lib
LINK : fatal error LNK1104: cannot open file 'glut32.lib'
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\link.EXE"' : return code '0x450'
Stop.
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\nmake.EXE"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\BIN\nmake.EXE"' : return code '0x2'
Stop.
From what I understand, glut32.lib should be compiled and created in {source}\lib\glut\ but it's not there.
Any help compiling a static library for glut would be appreciated.
Edit:
In Makefile.win
Changed:
$(link) $(LFLAGS) -out:$(GLUTDLL) -def:glut.def $(OBJS) $(LIBS)
to
lib $(OBJS) $(LIBS)
It generated \lib\glut\glut_8x13.lib not sure what the 8x13 is about but when I tried to link with it, I got some unresolved externals:
GPURenderer_test.obj : error LNK2019: unresolved external symbol __imp__glutCreateWindow#4 referenced in function "protected: __thiscall GlobalDecl::OpenGLConte
xt::OpenGLContext(unsigned int,unsigned int)" (??0OpenGLContext#GlobalDecl##IAE#II#Z)
GPURenderer_test.obj : error LNK2019: unresolved external symbol __imp__glutInitWindowSize#8 referenced in function "protected: __thiscall GlobalDecl::OpenGLCon
text::OpenGLContext(unsigned int,unsigned int)" (??0OpenGLContext#GlobalDecl##IAE#II#Z)
GPURenderer_test.obj : error LNK2019: unresolved external symbol __imp__glutInitWindowPosition#8 referenced in function "protected: __thiscall GlobalDecl::OpenG
LContext::OpenGLContext(unsigned int,unsigned int)" (??0OpenGLContext#GlobalDecl##IAE#II#Z)
GPURenderer_test.obj : error LNK2019: unresolved external symbol __imp__glutInitDisplayMode#4 referenced in function "protected: __thiscall GlobalDecl::OpenGLCo
ntext::OpenGLContext(unsigned int,unsigned int)" (??0OpenGLContext#GlobalDecl##IAE#II#Z)
GPURenderer_test.obj : error LNK2019: unresolved external symbol __imp__glutInit#8 referenced in function "protected: __thiscall GlobalDecl::OpenGLContext::Open
GLContext(unsigned int,unsigned int)" (??0OpenGLContext#GlobalDecl##IAE#II#Z)
GPURenderer_test.obj : error LNK2019: unresolved external symbol __imp__glutSwapBuffers#0 referenced in function "protected: void __thiscall GlobalDecl::OpenGLC
ontext::_executeTest(class GlobalDecl::UnitTest &)" (?_executeTest#OpenGLContext#GlobalDecl##IAEXAAVUnitTest#2##Z)
Thanks,
Ryan
It looks to me like you have something pretty badly munged up in your build setup (or you're using the wrong build setup completely, such as trying to build some samples before you've built the library they use).
To build a static library, you shouldn't be running link at all. For a static library, you compile your object files, then use lib to put them together into a library. You'd use link to build a dynamic library (DLL) or an executable, but not a static library.