0

I have a big project where I want to introduce a new library, both are mantained by me so full access to the code, CMakeLists etc

In Linux it is really easy, I just install the library with make install and then add it to my project with find_package(mynewlib REQUIRED). Then I use it as any other external library, target_link_libraries, add the headers and everything goes fine.

In Windows, I cannot make install, so I add it as a git submodule and use it like this:

    find_package(MYLIB QUIET)
    if(MYLIB_FOUND)
        set_property(GLOBAL APPEND PROPERTY CUSTOM_STATUS MYLIB)
        set_property(GLOBAL APPEND PROPERTY CUSTOM_STATUS_MYLIB
            "   MYLIB:" "version ${MYLIB_VERSION}")
    else()
        add_subdirectory(modules/mylib)
    endif()

    set(ALL_LIBS
        Qt5::Core
        Qt5::Widgets
        MYLIB)

It doesn't work, linker starts complaining about unresolved symbols in different parts of the code, not related with the new introduced library. For example:

[build] Main.cpp.obj : error LNK2019: unresolved external symbol "int __cdecl qInitResources_application(void)" (?qInitResources_application@@YAHXZ) referenced in function main [build] Main.cpp.obj : error LNK2001: unresolved external symbol "public: static struct Version const Version::SOFTWARE_VERSION" (?SOFTWARE_VERSION@Version@@2U1@B) [build] targetapp\targetappcore.dll : fatal error LNK1120: 52 unresolved externals

My question is, is my approach correct? MYLIB has a very big CMakeLists.txt I am currently analyzing in case it conflicts with the top level one, but if it was fine, is this how to do it?

I will consider using a package manager like vcpkg or conan in the future, but right now I would like to keep with git submodules.

Daniel Viaño
  • 485
  • 1
  • 9
  • 26
  • 1
    Is `MYLIB` a *static* or a *dynamic* library? If it's static, you have to remember that a static library is really nothing more than an archive of object files, and linking with a static library is like linking with the object files themselves. As such, if the library have dependencies you need to link with those too. – Some programmer dude Aug 03 '22 at 06:19
  • It is a dynamic library – Daniel Viaño Aug 03 '22 at 06:29
  • 1
    What *are* the "unresolved symbols" you have? And how are they (or your code where you get the errors) "not related with the new ... library"? When asking questions about build errors, please include the actual errors. And if possible a [mre] as well. – Some programmer dude Aug 03 '22 at 06:48
  • I have added a sample of the errors. With not related I mean that the unresolved external symbols that can't be found are not from the library I try to import. – Daniel Viaño Aug 03 '22 at 12:27
  • I was trying to create a minimal example but it works importing a "hello world" library, so I guess I have to debug MYLIB CMakeLists.txt – Daniel Viaño Aug 03 '22 at 12:29
  • 1
    It's your Qt configuration which is wrong (the hint is the leading `q` in e.g. `qInitResources_application`). A quick search seems to indicate that you need to set up your Qt resource file properly and add it to your executable target. See e.g. [undefined reference to `qInitResources_systray()`](https://stackoverflow.com/questions/35656489/undefined-reference-to-qinitresources-systray) or [Undefined reference to qInitResources](https://forum.qt.io/topic/88959/undefined-reference-to-qinitresources). – Some programmer dude Aug 03 '22 at 12:56
  • *"In Windows, I cannot make install, so I add it as a git submodule and use it like this"* Why? You claim to have full control of both libs, so you ***can*** implement `install` logic that works for both linux and windows. If you've got problems with file permissions, simply run the install command in a command window started as admin or use a dedicated directory you've for write access for as root of the installation. – fabian Aug 03 '22 at 18:11
  • `cmake --install [--prefix ] [--config ]` should work for all generators in case you're just looking for the windows equivalent of `make install`. (You may need to provide `CMAKE_PREFIX_PATH` and/or `CMAKE_MODULE_PATH` for the project consuming the lib.) – fabian Aug 03 '22 at 18:13

0 Answers0