0

In this time, I want to use Freeopcua to create a library to use in other project.

I have install all the tools in debian.soft file and build with following command sudo sh build.sh, cmake ., make, sudo make install. Although, in directory /usr/local/include and /usr/local/lib, there are opc directory and libopc*.so respectively, when I create a new cmake project, there is an error. Hope someone can help me or give some suggest. Thanks everyone~~~

I have refer to Libraries in /usr/local/lib not found, but it doesn't work for me

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)

project(OPCUAIndependent)

include_directories(/usr/local/include/)
link_directories(/usr/local/lib/)

add_executable(sourceCode sourceCode.cpp)

target_link_libraries(sourceCode opc)

Error

[ 50%] Linking CXX executable sourceCode
/usr/bin/ld: cannot find -lopc
collect2: error: ld returned 1 exit status
CMakeFiles/sourceCode.dir/build.make:96: recipe for target 'sourceCode' failed
make[2]: *** [sourceCode] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/sourceCode.dir/all' failed
make[1]: *** [CMakeFiles/sourceCode.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2
hpshboss
  • 23
  • 2
  • In the `target_link_libraries` command you should specify **exact name** of the library you want to link. If you want to link with `opcuaprotocol` library, use `target_link_libraries(sourceCode opcuaprotocol)`, if you want to link with `opcuacore` library, use `target_link_libraries(sourceCode opcuacore)`, if you want to link with both of them, use `target_link_libraries(sourceCode opcuacore opcuaprotocol)`. – Tsyvarev May 24 '21 at 11:37
  • Thank you, the error disappear, but there still some error when I execute binary file. `malloc.c:2401: sysmalloc: Assertion` – hpshboss May 24 '21 at 12:30
  • Therefore, I find when I use `if(NOT opcuaprotocol_FOUND) find_package(opcuaprotocol REQUIRED) endif()` in CMakeList.txt, it say Could not find a package configuration file provided by "opcuaprotocol" with any of the following names `opcuaprotocolConfig.cmake` `opcuaprotocol-config.cmake` – hpshboss May 24 '21 at 12:35
  • According to the project's [CMakeLists.txt](https://github.com/FreeOpcUa/freeopcua/blob/master/CMakeLists.txt#L613), the name of the package to search is `FreeOpcUa`. E.g. `find_package(FreeOpcUa REQUIRED)`. This call creates targets like `opcuacore` or `opcuaprotocol`, so you could use them in the `target_link_libraries` call. – Tsyvarev May 24 '21 at 12:43
  • Thank you a lot, it works for me. Finally, I just modify origin `CMakeLists.txt` including deleting `add_library`, and adding `find_package(FreeOpcUa REQUIRED)`...,etc, instead of creating a new ‵CMakeLists.txt‵. – hpshboss May 24 '21 at 13:06

0 Answers0