2

I am trying to add an external library, called OpenAlpr, with alpr.h and openalpr.lib/openalpr.dll files to my CLion project. I put the header file in my project directory and included it in the source code, but I have no idea how to add the .dll or .lib files. I looked at other answers like this and this, but they are too confusing for me and I could not get mine to work; the following errors are output when I try to run:

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

These are the contents of my CMake file:

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)

Thanks in advance.

sepehr78
  • 71
  • 2
  • 10
  • Possible duplicate of [CMake link to external library](https://stackoverflow.com/questions/8774593/cmake-link-to-external-library) – Tsyvarev Sep 10 '18 at 11:54
  • Actually finding the lib is great But it takes the var name and not a target. See : https://cmake.org/cmake/help/v3.0/command/find_library.html You should call it this way Find_library(OpenALPR_LIBS openalpr.lib) It will store the library path in OpenALPR_LIBS Then you have to link against With target_link_libraries. See https://cmake.org/cmake/help/v3.0/command/target_link_libraries.html?highlight=target%20link%20libraries#command:target_link_libraries With this you should be able to compile. – Noki Sep 12 '18 at 17:29
  • But it ll crash at runtime because of missing dll you should now either copy it to the executable path or set an RPATH or add dll path to PATH env. Tell me if you need a way more detailed answer – Noki Sep 12 '18 at 17:30

1 Answers1

2

Do not copy the header files of your libraries to your project.

At some point you will need to go through tutorials the hard way and learn how to use find_package. If you just want to get thing to work quickly, here is one simplistic way (assuming file names Alpr.lib Alpr.h) .

cmake_minimum_required(VERSION 3.12)
project(AlprCpp)

set(ALPR_LIBRARY "" CACHE FILEPATH  "Full path to Alpr.lib")
set(ALPR_INCLUDE_PATH "" CACHE PATH "Directory containing Alpr.h")
include_directories(${ALPR_INCLUDE_PATH})

add_executable(AlprCpp main.cpp)
target_link_libraries(AlprCpp ${ALPR_LIBRARY })

As for the dll files (assuming Windows), either set the PATH environment variable or copy the DLL to the directory where the .exe is located.

André Aichert
  • 313
  • 2
  • 11
  • I tried what you suggested, but the same error is being output. – sepehr78 Sep 10 '18 at 10:58
  • 1
    Have you set ALPR_LIBRARY and ALPR_INCLUDE_PATH in cmake ?You can also post earlier output like "undefined referece to ..." to find out exactly which functions are undefined. I am not familiar with Alpr, is there more than one .lib file? – André Aichert Sep 10 '18 at 11:01
  • I added the earlier part of the output. It's giving errors about all the functions in the header file, saying that they are undefined. The library has a few other .lib files, and I put them all in the same INCLUDE_DIRECTORY. – sepehr78 Sep 10 '18 at 15:46
  • https://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work – André Aichert Sep 10 '18 at 21:02
  • Your problem has nothing to do with #include's and header files and everything to do with the linker. You have to link *all* .lib files which are used. It also has nothing to do with ".dll" files because that's a runtime thing. I just looked at OpenAlpr and it is too complex to get it to work by hand like I suggested. please get acquanted with CMake's find_package. – André Aichert Sep 10 '18 at 21:08
  • If it is too complex then how am I supposed to use it? – sepehr78 Sep 17 '18 at 10:25
  • Please get acquanted with CMake's find_package. – André Aichert Sep 18 '18 at 12:07