使用带有CMake和Conan的外部库的未定义参考 [英] Undefined reference using external library with CMake and Conan

查看:288
本文介绍了使用带有CMake和Conan的外部库的未定义参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个程序,该程序使用带有LibLogicalAccess库的Conan和CMake与PCSC USB阅读器通信。我遵循了建立和安装该库的指示,看起来似乎还不错。我使用 main.cpp文件创建了一个小的简单控制台项目。按照我尝试的图书馆Wiki上的 C ++指南从库中调用一个函数,导致对 function 的未定义引用。我知道有很多关于此的主题,但是我已经读了很多,但似乎找不到正确的解决方案。

I am trying to develop a program that communicates with a PCSC USB reader using Conan and CMake with the LibLogicalAccess library. I followed the instructions of building and installing the library which seemed to have gone fine. I created a small simple console project with a "main.cpp" file. Following the C++ guide on the wiki of the library I tried to call a function from the library which resulted in a "Undefined reference to function. I know there are a lot of topics covering this but I have read as many as I could but could not seem to find the right solution.

我对Ubuntu / CMake / Conan / C ++没有太多经验,所以它可能也是一个非常简单的解决方法。

I don't have much experience with Ubuntu/CMake/Conan/C++ so it might as well be a very simple fix.

操作系统:Kubuntu 18.04
语言:C ++
相关软件:LibLogicalAccess
2.2.1,
CMake 3.17.1,
柯南1.25.0

OS: Kubuntu 18.04 Lang: C++ Related software: LibLogicalAccess 2.2.1, CMake 3.17.1, Conan 1.25.0

main.cpp

#include <iostream>

#include <logicalaccess/dynlibrary/librarymanager.hpp>
#include <logicalaccess/readerproviders/readerconfiguration.hpp>
#include <logicalaccess/cards/chip.hpp>

int main()
{
    std::cout << "Program started\n";

    // Reader configuration object to store reader provider and reader unit selection.
    std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(new logicalaccess::ReaderConfiguration());

    // Set PCSC ReaderProvider by calling the Library Manager which will load the function from the corresponding plug-in
    readerConfig->setReaderProvider(logicalaccess::LibraryManager::getInstance()->getReaderProvider("PCSC"));

    std::cout << "after..\n";
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(project)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup(TARGETS)
set(CMAKE_CXX_FLAGS "-I /usr/include/PCSC")
add_executable(project main.cpp)
target_link_libraries(project PUBLIC CONAN_PKG::LogicalAccess)

当我尝试使用 cmake --build构建程序时。这是输出:

When I try to build the program using cmake --build . this is the output:

[100%] Linking CXX executable bin/project
CMakeFiles/project.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x140): undefined reference to `logicalaccess::LibraryManager::getReaderProvider(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:191: recipe for target 'bin/project' failed
make[2]: *** [bin/project] Error 1
CMakeFiles/Makefile2:95: recipe for target 'CMakeFiles/project.dir/all' failed
make[1]: *** [CMakeFiles/project.dir/all] Error 2
Makefile:103: recipe for target 'all' failed
make: *** [all] Error 2

奇怪的是第一行代码: std :: shared_ptr< logicalaccess :: ReaderConfiguration> ; readerConfig(...)正常工作,第二行代码给出了未定义的引用。

The weird part is that the first line of code: std::shared_ptr<logicalaccess::ReaderConfiguration> readerConfig(...) works fine and the second line of code gives an undefined reference.

我尝试了同一文件中的其他函数,但结果相同。当我删除最后的 setReaderProvider代码行时,文件将编译并运行良好。还对conanfile.txt和CMakeLists.txt进行了许多不同的小调整。

I have tried other functions in the same file which give the same result. The file compiles and runs fine when I remove the last "setReaderProvider" line of code. Also tried a lot of different little adjustments regarding the conanfile.txt and CMakeLists.txt.

推荐答案

您的错误提示:


main.cpp :(。text + 0x140):对`logicalaccess :: LibraryManager :: getReaderProvider(std :: __ cxx11 :: basic_string,std :: allocator> const&)'的未定义引用

main.cpp:(.text+0x140): undefined reference to `logicalaccess::LibraryManager::getReaderProvider(std::__cxx11::basic_string, std::allocator > const&)'

它的发生是因为您的CMake使用libstdc ++ 11进行链接,但是,由于向后兼容,Conan被配置为使用libstdc ++。
您需要更新默认的libcxx:

It occurs because your CMake is using libstdc++11 to link, however, Conan is configured to use libstdc++ due backward compatibility. You need to update your default libcxx:

conan profile update settings.compiler.libcxx=libstdc++11 default

请在柯南文档如何管理GCC ABI 以获取更多信息。

Please, read this section in Conan docs How to Manage GCC ABI to get more information.

入门的第5步中对此进行了说明。

Also, it's explained on step 5 of Getting Started.

现在,当再次构建时,您将需要本地软件包不可用,因为它是使用不同设置的新软件包,因此您需要安装或从源进行构建。到libstdc ++ 11的链接由柯南自动管理,它将定义传递给CMake。

Now when building again, you will need that your local packages won't be available, because it's a new package, using different settings, so you will need to install, or build from sources. The link to libstdc++11 is automatically managed by Conan, it passes the definitions to CMake.

致谢!

这篇关于使用带有CMake和Conan的外部库的未定义参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆