Reputation: 315
[Background] I have to move some .cpp files out of repo [repo2] to new repo[repo1]. To achieve this, I took these cpp files from repo2 and made a library (xyz.lib in repo1) as follow.
[In repo1]
add_library(XYZ PUBLIC stdafx.h
stdafx.cpp
m.cpp
p.cpp)
target_link_libraries(XYZ CONAN_PKG::SDK)
set_source_files_properties(stdafx.cpp PROPERTIES COMPILE_FLAGS "/Yc")
target_include_directories(XYZ PRIVATE
${CMAKE_SOURCE_DIR}/<some_header>
)
[In repo2] - This library from repo1 is available to repo2 through conan-package. This is how it's consumed :
add_library(M_TARGET SHARED
#m.cpp #These files commented out as, its now available through lib from repo1
#p.cpp #These files commented out as, its now available through lib from repo1
m.h
p.h
stdafx.cpp)
target_link_libraries(M_TARGET
CONAN_PKG::XYZ)
Problem : While I am compiling the repo2, its not able to link the lib available in CONAN_PKG::XYZ
to build target M_TARGET
.
What I tried?
Cmake target_link_libraries not linking my library 2. Tried to change library from public to static and vice versa, din't work.
Here's conanbuildinfo.cmake :
set(CONAN_LIB_DIRS_XYZ "<PATH>/lib")
Directory structure:
<PATH>/lib/debug/XYZ.lib
<PATH>/lib/release/XYZ.lib
Even if I put XYZ.lib
in <PATH>/lib
, I get linking error.
Error :
error LNK2001: unresolved external symbol
M_TARGET.dll : fatal error LNK1120: 2 unresolved externals
If I browse to properties in visual studio and manually add the library in linker input, it compiles. What am I missing ? Please help as this is blocking my work.
[Adding more logs as asked]:
37>Link:
Creating library C:/build/install/lib/RelWithDebInfo/M_TARGET.lib and object C:/build/install/lib/RelWithDebInfo/M_TARGET.exp
37>communication_handler.obj : error LNK2001: unresolved external symbol "public: int __cdecl xy_z::XYServiceZ::Init(char const *)" (?Init@XYServiceZ@rxy_z@@QEAAHPEBD@Z) [C:\build\src\M_TARGET\M_TARGET.vcxproj]
37>communication_handler.obj : error LNK2001: unresolved external symbol "public: __cdecl xy_z::XYServiceZ::XYServiceZ(class dx::Dupe &)" (??0XYServiceZ@xy_z@@QEAA@AEDupe@dx@@@Z) [C:\build\src\M_TARGET\M_TARGET.vcxproj]
63>CustomBuild:
Building Custom Rule C:/src/libs/ScanLib/test/unit/scan_manager_test/CMakeLists.txt
CMake does not need to re-run because C:\build\src\libs\ScanLib\test\unit\scan_manager_test\CMakeFiles\generate.stamp is up-to-date.
37>C:\build\install\bin\RelWithDebInfo\M_TARGET.dll : fatal error LNK1120: 2 unresolved externals [C:\build\src\M_TARGET\M_TARGET.vcxproj]
[End of error]
Upvotes: 3
Views: 1797
Reputation: 315
In the conanbuildinfo.cmake
some of the PACKAGE_LIBS
path were not set. This happens when conanfile.py
from which the package was created does not contain the information about exact path of libs present in package. In such cases, we need to explicitly mention the name of the libs while creating the package.
For Eg:
If I have made a package with name CONAN_PKG::Remediation
and following directory structure :
Inside lib/
we have RemediationTest.lib
.
This lib is not recognized when we use this in target_link_libraries()
target_link_libraries(M_TARGET CONAN_PKG::Remediation)
. Since this does not recognize the lib/, it leads to unresolved external errors [Linking error].
Fix :
While creating package, in the conanfile.py
, add this :
conanfile.py:
def package_info(self):
self.cpp_info.libs = ["RemediationTest"]
Upvotes: 1