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)
Related
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})
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)
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
I've created a shared library with library.h and library.cpp. Then wrote a CMakeLists.txt file to build it as a shared library.
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(test_pro)
set(CMAKE_CXX_STANDARD 11)
add_library(test_pro SHARED library.cpp library.h)
after building the library, I was able to get a .so file as /home/user/projects/test_lib/bin/libtest_pro.so
Then I tried linking the created library to another project in /home/user/projects/testproject
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(testproject)
set(CMAKE_CXX_STANDARD 11)
link_directories(
/home/user/projects/test_lib/bin
)
add_executable(testproject main.cpp)
target_link_libraries (testproject test_pro)
It successfully builds the testproject (ldd command shows it has linked correctly), but I'm unable to use the library I've created in it.
In the main.cpp I've tried,
#include "library.h"
#include "test_pro"
#include <test_pro>
#include <test_pro/library.h>
But all the above gave build failures (fatal error: xxx: No such file or directory). How do I use this created library?
Just like you set link_directories() you have to specify include_directories(). And it is recommended not to use link_directories() at all, instead pass absolute path to the library into target_link_libraries().
Suppose C++ project A as my main program and project B is my module that is shared object (.so). Project A consist source and headers that is common between both projects. Project A load module .so file in runtime.
Project A:
class-AI.cpp (It contains function funcA)
class-AI.h
class-AII.cpp
class-AII.h
class-AIII.cpp
class-AIII.h
main.cpp
Project B: (Shared Object)
class-BI.cpp (Its inherit from class-AI)
class-BI.h
With following CMake files both project build successfully.
Project A CMakefile:
cmake_minimum_required(VERSION 3.6)
project(ProjectA)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES
main.cpp
class-AI.cpp
class-AI.h
class-AII.cpp
class-AII.h
class-AIII.cpp
class-AIII.h)
add_executable(ProjectA ${SOURCE_FILES})
set(EXECUTABLE_OUTPUT_PATH /opt/ProjectA/bin/)
Project B CMakefile:
cmake_minimum_required(VERSION 3.6)
project(ProjectB)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES class-BI.cpp class-BI.h)
add_library(ProjectB SHARED ${SOURCE_FILES})
set_target_properties(ProjectB PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY /opt/ProjectA/bin/)
But at runtime Project A following error will be appear:
undefined symbol: _ZN6class-AI4funcAEv
How can I add dependency to project B correctly using CMake file and solve this error? (Actually I want to change my netbeans C++ project to Jetbrains CLion)
Thanks to Florian for his comment. My problem solved with adding this lines to Project A CMake file:
add_library(ProjectB SHARED IMPORTED GLOBAL)
set_target_properties(ProjectB PROPERTIES IMPORTED_LOCATION "/opt/ProjectA/bin/ProjectB.so")
target_link_libraries(ProjectA ProjectB)