【发布时间】:2018-09-10 10:32:20
【问题描述】:
我正在尝试向我的 CLion 项目添加一个名为 OpenAlpr 的外部库,其中包含 alpr.h 和 openalpr.lib/openalpr.dll 文件。我将头文件放在我的项目目录中并将其包含在源代码中,但我不知道如何添加 .dll 或 .lib 文件。我查看了其他答案,例如 this 和 this,但它们对我来说太混乱了,我无法让我的工作;当我尝试运行时输出以下错误:
undefined reference to `alpr::Alpr::Alpr(std::string, std::string, std::string)'
.text+0x9f): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `alpr::Alpr::Alpr(std::string, std::string, st
undefined reference to `alpr::Alpr::getVersion()'
(.text+0xf3): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `alpr::Alpr::getVersion()'
undefined reference to `alpr::Alpr::~Alpr()'
.text+0x123): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `alpr::Alpr::~Alpr()'
undefined reference to `alpr::Alpr::~Alpr()'
.text+0x1af): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `alpr::Alpr::~Alpr()'
collect2: error: ld returned 1 exit status
make[3]: *** [CMakeFiles/AlprCpp.dir/build.make:84: AlprCpp.exe] Error 1
make[2]: *** [CMakeFiles/Makefile2:73: CMakeFiles/AlprCpp.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:85: CMakeFiles/AlprCpp.dir/rule] Error 2
make: *** [Makefile:118: AlprCpp] Error 2
这些是我的 CMake 文件的内容:
cmake_minimum_required(VERSION 3.12)
project(AlprCpp)
set(CMAKE_CXX_STANDARD 14)
add_executable(AlprCpp main.cpp alpr.h)
link_directories(C:\\Projects\\AlprCpp)
find_library(AlprCpp openalpr.lib)
提前致谢。
【问题讨论】:
-
实际上找到lib很棒但是它需要var名称而不是目标。请参阅:cmake.org/cmake/help/v3.0/command/find_library.html 你应该这样称呼它 Find_library(OpenALPR_LIBS openalpr.lib) 它将库路径存储在 OpenALPR_LIBS 然后你必须链接到与 target_link_libraries。见cmake.org/cmake/help/v3.0/command/…有了这个你应该可以编译了。
-
但是由于缺少 dll,它会在运行时崩溃,您现在应该将其复制到可执行路径或设置 RPATH 或将 dll 路径添加到 PATH env。如果您需要更详细的答案,请告诉我