ld: library not found for -lbitcoin - c++

I am trying to add libbitcoin library to my c++ project. I am using Cmake. I found an example for a configuration here: Use libbitcoin in CLion. Here is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.14)
project(vuchain)
set(CMAKE_CXX_STANDARD 11)
set(ENV{PKG_CONFIG_PATH} "/usr/local/libbitcoin/lib/pkgconfig/:$ENV{PKG_CONFIG_PATH}")
find_package(PkgConfig REQUIRED)
find_library(libbitcoin PKG_CONFIG_PATH)
pkg_check_modules(LIB_BITCOIN REQUIRED libbitcoin)
add_executable(vuchain main.cpp user.h main.h ./HASH/HASH.cpp ./HASH/HASH.h ./HASH/sha256.cpp ./HASH/sha256.h transaction.h transaction_list.h block.h HASH/sha256.cpp HASH/sha256.h blockchain.cpp blockchain.h)
target_link_libraries(vuchain PRIVATE ${LIB_BITCOIN_LIBRARIES})
target_include_directories(vuchain PRIVATE ${LIB_BITCOIN_INCLUDE_DIRS})
CMakeLists.txt does not throw any errors on save, but when compiling, I am getting ld: library not found for -lbitcoin error. Is there is sotheming I am missing here?

Use the IMPORTED_TARGET option to pkg_check_modules.
This option generates a CMake target you can link against, and it will make sure to set all relevant variables for you.
pkg_check_modules(LIB_BITCOIN REQUIRED IMPORTED_TARGET libbitcoin)
target_link_libraries(vuchain PRIVATE PkgConfig::LIB_BITCOIN)

Related

How to properly link to libraries in CMake (using Boehm GC)?

I've been trying to figure this out for a while, but can't seem to get it working. I've already checked a bunch of posts.
I have a static library libRuntime.a, generated like so:
cmake_minimum_required(VERSION 3.15)
project(Runtime)
set(CMAKE_CXX_STANDARD 17)
add_library(Runtime library.cpp)
find_library(GC gc)
message(${GC})
target_link_libraries(Runtime PUBLIC ${GC})
library.cpp uses Boehm GC, which is why I'm also linking it with my Runtime target.
Now, I want to call functions from my libRuntime.a, so I have the following other CMake project:
cmake_minimum_required(VERSION 3.15)
project(test)
set(CMAKE_CXX_STANDARD 17)
add_executable(test main.cpp)
find_library(TESTLIB Runtime lib)
message(${TESTLIB})
target_link_libraries(test ${TESTLIB})
I have pasted library.h into the project, and also pasted libRuntime.a into a directory called lib, so the definitions are known and the library is found. Calling functions from my Runtime library now gives me:
/path.../Scheme-Compiler/Runtime/library.cpp:12: undefined reference to `GC_init'
/usr/bin/ld: ../lib/libRuntime.a(library.cpp.o): in function `alloc_atom':
Thanks in advance
Because you use a separate CMake invocation to create the executable, the properties of the Runtime CMake target from your first project are not known. Specifically, CMake will not know any of the Runtime library's dependencies (i.e. GC), so you have to list them explicitly when linking Runtime to your executable:
cmake_minimum_required(VERSION 3.15)
project(test)
set(CMAKE_CXX_STANDARD 17)
add_executable(test main.cpp)
find_library(TESTLIB Runtime lib)
message(${TESTLIB})
# Find GC library.
find_library(GC gc)
# Link GC here, along with the Runtime library.
target_link_libraries(test PRIVATE ${GC} ${TESTLIB})

Trying to add libraries with CMake results in error

I am trying to add an external .lib file to my project in Clion which uses CMake. My code is very simple and is simply to test whether the library gets included:
#include <iostream>
#include "header/test.h"
int main() {
test a; // returns error saying undefined reference to 'test::test()'
return 0;
}
When running this code I get the following error:
undefined reference to `test::test()'
This is because I am trying to make a test object however the library for test is not included.
The test.lib file and the test.h file are both in the "header" folder which is in the root of my project folder. The file path to this is F:\Project\header\
My Cmake text file is as follows:
cmake_minimum_required(VERSION 3.14)
project(Project)
set(CMAKE_CXX_STANDARD 14)
add_executable(Project main.cpp)
target_link_libraries(Project
F:\\Project\\header\\test.lib)
In the cmake text file i use the line:
target_link_libraries(Project F:\Project\header\test.lib)
This should include the library file, however it doesn't seem to because I get the "undefined reference to..." error as mentioned above. The Cmake compiler does not give me an error.
You are conceptually correct, however you are not doing it in the CMake fashion. Check out the following links on how to link an external library.
CMake link to external library
cmake doesn't support imported libraries?
https://gitlab.kitware.com/cmake/community/wikis/doc/tutorials/Exporting-and-Importing-Targets
For your case, it would be as follows):
cmake_minimum_required(VERSION 3.14)
project(Project)
set(CMAKE_CXX_STANDARD 14)
# Import the library into the CMake build system
ADD_LIBRARY(test SHARED IMPORTED)
# Specify the location of the library
SET_TARGET_PROPERTIES(TARGET test PROPERTIES IMPORTED_LOCATION “/path/to/lib/test.dll”)
# create the executable
add_executable(Project main.cpp)
# Link your exe to the library
target_link_libraries(Project test)
The CMake documentation is very good. I recommend checking it out when you run into issues.
https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries

Compiling with CMake and Boost unit tests

I'm relatively new to C++, and I'm trying to compile a project using CMake with the following #include:
#include <boost/test/unit_test.hpp>
I get the following error
undefined symbols for architecture x86_64:
"boost::unit_test::unit_test_log_t::instance()", referenced from:
___cxx_global_var_init in functions1.cpp.o
___cxx_global_var_init in functions2.cpp.o
___cxx_global_var_init in main.cpp.o
ld: symbol(s) not found for architecture x86_64
I'm pretty sure this is something to do with my CMakeLists.txt, so here it is:
cmake_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
include_directories(.)
include_directories(/usr/local/include/)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")
find_package(Boost COMPONENTS filesystem system unit_test_framework REQUIRED)
#find_package(Boost 1.69.0)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
include_directories (${Boost_INCLUDE_DIRS})
I'm on OSX Mojave, and installing with brew install boost. I'm aware there are several posts out there that report very similar issues, but none of the solutions seem to work for me.
Edit:
Have adjusted my CMakeLists to the following based on Guillaume's suggestion below.
make_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
set(BOOST_ROOT "/usr/local/Cellar/boost/1.69.0_2")
find_package(Boost COMPONENTS filesystem system test REQUIRED)
target_include_directories(MyProject PUBLIC ".")
target_link_libraries(MyProject PUBLIC
Boost::filesystem Boost::system Boost::test)
I understand that this is, in principle, better, but it's giving me:
Unable to find the requested Boost libraries.
Boost version: 1.69.0
Boost include path: /usr/local/Cellar/boost/1.69.0_2/include
Could not find the following Boost libraries:
boost_test
Some (but not all) of the required Boost libraries were found. You may need to install these additional Boost libraries. Alternatively, set BOOST_LIBRARYDIR to the directory containing Boost libraries or BOOST_ROOT to the location of Boost.
Edit 2:
Have tried edits and upgrading to boost 1.7, but still have:
Could not find a package configuration file provided by "boost_test"
(requested version 1.70.0) with any of the following names:
boost_testConfig.cmake
boost_test-config.cmake
Add the installation prefix of "boost_test" to CMAKE_PREFIX_PATH or set
"boost_test_DIR" to a directory containing one of the above files. If
"boost_test" provides a separate development package or SDK, be sure it
has been installed.
You have to link properly to boost instead of adding include directory flags.
Linking libraries in cmake will apply all required property to use boost onto your targets.
First off, don't do that:
include_directories(.)
include_directories(/usr/local/include/)
This is asking for link error. It will enable you to use headers of libraries you don't link properly to. This will cause linking errors, even within your own project.
cmake_minimum_required(VERSION 3.13)
project(MyProject)
set(CMAKE_CXX_STANDARD 14)
add_executable(MyProject
functions1.cpp
functions1.h
functions2.cpp
functions2.h
main.cpp
main.h
utillity.cpp
utillity.h)
list(APPEND CMAKE_PREFIX_PATH "/usr/local/Cellar/boost/1.69.0_2")
set(Boost_ADDITIONAL_VERSIONS "1.69.0" "1.69")
find_package(Boost COMPONENTS filesystem system test REQUIRED)
# Not needed
#if(NOT Boost_FOUND)
# message(FATAL_ERROR "Could not find boost!")
#endif()
# include_directories (${Boost_INCLUDE_DIRS})
target_include_directories(MyProject PUBLIC ".")
# adds include directories, definitions and link libraries
target_link_libraries(MyProject PUBLIC
Boost::filesystem Boost::system Boost::test
)

linking to self created dll fails

using CLION i did create a very basic "C++ Library" containing only one function. Library Type is "shared".
in Library.h there is only a
void hello();
function declaration.
Library.cpp contains the definition
void hello(){std::cout << "hello";}
When i compile it, i get 2 files: libLibrary.dll and libLibrary.dll.a.
Here comes my problem. When i create a new Project named Test (an executable), i can include the Library.h file with no problems. But it wont compile due to an "undefined reference to `hello()'" which is no surprise, because i did not link to the created DLL-File. I added "target_link_libraries" to the CMakeLists.txt and copied both DLL-Files to the same directory where the executable is to be build.
The CMakeLists file looks as follow:
cmake_minimum_required(VERSION 3.12)
project(Test)
set(CMAKE_CXX_STANDARD 17)
add_executable(Test main.cpp)
target_link_libraries(Test Library)
its always the same error-message:
C:/PROGRA~2/MINGW-~1/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686/w64-mingw32/bin/ld.exe: cannot find -lLibrary
I did also try
target_link_libraries(Test libLibrary)
target_link_libraries(Test -libLibrary)
target_link_libraries(Test -Library)
target_link_libraries(Test libLibrary.dll)
target_link_libraries(Test Library.dll)
target_link_libraries(Test -libLibrary.dll)
...to no avail.
What did i miss?
i could finally link against the .dll-File by specifying the concrete location of the .dll-file, as #drescherjm suggested. Here linking to the .dll-File was successful with the command
target_link_libraries(Test ${CMAKE_BINARY_DIR}/libLibrary.dll)

Include Eigen library for Xcode project via CMake/CMakeLists.txt

I've got the following CMakeLists.txt (in my "project" folder) file for my project.
# define new project
PROJECT(SETUPMARKERTEST)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
if(UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif(UNIX)
# Set static build for GLFW
SET(BUILD_SHED_LIBS OFF)
ADD_SUBDIRECTORY(ext/glfw-3.1.1)
# Set shared lib build for the rest
SET(BUILD_SHARED_LIBS ON)
# Find dependencies
SET(EIGEN_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/ext/Eigen-3.1.2")
FIND_PACKAGE(OpenCV REQUIRED)
# Set header and source files
SET(MAR_Test_SOURCES
src/main.cpp
src/MarkerTracker.h src/MarkerTracker.cpp
src/PoseEstimation.h src/PoseEstimation.cpp
)
# define executable
ADD_EXECUTABLE(${PROJECT_NAME} ${MAR_Test_SOURCES})
# define additional include directories and linking targets
INCLUDE_DIRECTORIES("ext/glfw-3.1.1/include" ${EIGEN_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} glfw ${OPENGL_glu_LIBRARY} ${GLFW_LIBRARIES})
And my Eigen folder is in "project/ext/Eigen/3.1.2/Eigen/".
Somehow when I created my project for Xcode (in "project/buildXcode/" with Cmake .. -G "Xcode") and run it, Xcode throws me the error:
So I guess there is some error in my CMakeLists.txt, unfortunately I received that file and I'm new to CMake and thus didn't write it on my own nor am I very skilled with CMake.
Do you know what causes the error and can you fix the CMakeLists.txt that my project runs with the Eigen library?
Unfortunately it looks like windows is having no problem with this, whereas mac is bleating.
You just have to use
#include <Eigen/Dense>
instead of
#include <Eigen\Dense>
...pretty stupid error.