3

[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?

  1. I tried to follow other links but I could not find anything that works. cmake does not link conan's installed library

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]

0726
  • 315
  • 2
  • 13
  • Please show a [mre]. What is the full error message? Which library are the missing symbols defined in? – Alan Birtles Jul 09 '21 at 21:47
  • It would be necessary to see in that minimal reproducible example also the conanfile.py files (not only the sources and CMakeLists.txt) that you are using to create your package xyz, is it possible that something is missing in xyz recipe. – drodri Jul 09 '21 at 22:00
  • I have tried to add the error msg, not sure how helpful it is. @AlanBirtles : symbols are in XYZ lib. – 0726 Jul 09 '21 at 22:08
  • @drodri : I am assuming the lib is packaged correctly because when I am doing manual linking , it works. Would you still need conanfile.py ? – 0726 Jul 09 '21 at 22:08
  • @AlanBirtles - I am probably missing something in the repo2's cmake which I am not sure.. otherwise manual linking should have failed too. – 0726 Jul 09 '21 at 22:11
  • We still need a [mre] – Alan Birtles Jul 10 '21 at 05:22
  • I am unable to provide you more details @AlanBirtles . After some research I found that some of the CONAN_LIB paths are not set "set(CONAN_LIBS_XYZ_DEBUG xyz.lib)" for lib that I am trying to consume. I have made sure that I am using same profile for building/consuming this package. Can you provide me hint as to why some CONAN_LIB path may not be set while others do ? – 0726 Jul 11 '21 at 21:24
  • If you can provide a [mre] then we can help otherwise you'll have to find the problem yourself – Alan Birtles Jul 11 '21 at 21:55
  • @0726, yes, the information passed to consumers is declared in the recipe ``package_info()`` method. If something is missing there, the consumers will not know how to link with the package. So knowing the ``conanfile.py`` is important. – drodri Jul 11 '21 at 22:02
  • Sorry, I could not provide all the details. But I found the root cause. Thanks for all the help - AlanBirtles and drodri. I will post the answer and we can close this thread. – 0726 Jul 12 '21 at 13:04
  • @AlanBirtles: can you please accept the answer and close this thread ? – 0726 Jul 12 '21 at 19:19
  • It's your question, you need to accept the answer, nothing to do with me – Alan Birtles Jul 12 '21 at 19:50

1 Answers1

1

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 : enter image description here

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"]
0726
  • 315
  • 2
  • 13