How to use dynamic link library with CMake? - c++

I have simple program as follow:
CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
LINK_DIRECTORIES(${PROJECT_SOURCE_DIR})
add_executable(test main.cpp)
target_include_directories(test PRIVATE ${PROJECT_SOURCE_DIR})
target_link_libraries(test PRIVATE power.dll)
main.cpp:
#include <iostream>
#include "power.h"
using namespace std;
int main()
{
cout << "Hello World!" << endl;
power(4.);
return 0;
}
power.h:
#ifndef POWER_H
#define POWER_H
double power(double number) noexcept;
#endif // POWER_H
Implementation of power.h is in a .dll named power.dll.
If I compile this project with MinGW 7.3.0 X64 says:
error: undefined reference to `power(double)'
If I compile it with MSVC 2017 X64 says:
error: LNK1104: cannot open file 'power.lib'
both errors show that power.dll can't detect by the linker.
I did many searches but none of solutions worked for me!
Can anyone help about this?
Thanks in advance!

Your modelling of the dynamic library is incorrect, both on CMake and on the source level.
As a starting point, try building the dll as part of the same CMake project as the consuming executable:
cmake_minimum_required(VERSION 3.5)
project(test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(GenerateExportHeader)
add_library(power SHARED power_sources.cpp power.h)
generate_export_header(power)
target_include_directories(power PUBLIC ${PROJECT_BINARY_DIR} ${PROJECT_SOURCE_DIR})
add_executable(test main.cpp)
target_link_libraries(test PRIVATE power)
Note the use of the generate_export_header function, which instructs CMake to generate macros for exporting functions on shared library interfaces in a portable way. Since generated files go to the binary directory tree, we have to adjust the include directories for the library accordingly.
To make sure the function gets properly exported, change your header as follows:
#ifndef POWER_H
#define POWER_H
#include <power_export.h>
POWER_EXPORT double power(double number) noexcept;
#endif // POWER_H
Note that generare_export_header allows you to customize the generated export header extensively.
Be sure you get the project to build and run from this baseline.
If you want to build the dll externally (which is not strictly necessary, but since that's what your question is about...), we have to modify the CMake file to something like:
cmake_minimum_required(VERSION 3.5)
project(test LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(power)
add_executable(test main.cpp)
target_link_libraries(test PRIVATE power)
With all the magic here happening in the find_package call. That call is now responsible for providing all the information that was previously handled by the lines for building the library:
Providing of an imported target power for consumption by the target_link_libraries call
Association of the library name of the import library (the power.lib file) via that imported target
Exposure of the public include directories for both power.h and power_export.h via that imported target
You can either construct such an imported target manually in the find script, or have CMake do it for you. In the first case, create a FindPower.cmake script file, make sure it's location is part of the CMAKE_MODULE_PATH and write the code for finding the library and header files and constructing the imported target in there. Note that getting this right in a portable way can be very tricky and goes far beyond the scope of a StackOverflow question. In the second case, have the CMake script that builds the power library perform an install step during which a config file package will get generated, which can then be consumed by your test project. Note that this approach is not viable if the power library is not itself being built with CMake, so in that case you will have to stick with the first option.

Dynamic linking in Windows requires that externally visible symbols are declared with the keyword __declspec. Your header "power.h" should be modified:
#ifndef POWER_H
#define POWER_H
#if defined(__WIN32__) && !defined(__CYGWIN__)
# if (defined(_MSC_VER) || defined(__MINGW32__)) && defined(BUILD_DLL)
# define POWERAPI __declspec(dllexport)
# elif (defined(_MSC_VER) || defined(__MINGW32__))
# define POWERAPI __declspec(dllimport)
# endif
#endif
POWERAPI double power(double number) noexcept;
#endif // POWER_H
In the project building the DLL power.dll with CMake, you should define the symbol BUILD_DLL:
add_definitions(-DBUILD_DLL)
then it should generate a power.lib file when the MSVC compiler and a power.a when using MINGW. Don't define BUILD_DLL in the project using the DLL, and it should work.

Related

Require including <mylibrary/mylibrary.h> instead of <mylibrary.h> inside Cmake project that links to mylibrary

I have a very simple c++ shared library with a CMakeLists.txt file:
cmake_minimum_required(VERSION 3.20)
set(CMAKE_CXX_STANDARD 23)
project(mylibrary)
add_library (mylibrary src/mylibrary.h src/mylibrary.cpp)
target_include_directories(mylibrary PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src)
Now in the project that uses this library I link to this library with target_link_libraries(myapp mylibrary) and then I can include the library with #include <mylibrary.h>. However, to visually separate all library includes I want to (have to) use #include <mylibrary/mylibrary.h>, even though mylibrary.h is not inside a folder called mylibrary. Is there a way to achieve this with Cmake?

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})

DLL Made with CMake makes the program crash

I'm struggling to use a DLL generated using CMAKE and C++. I'm able to build the library, include it and build the target project, the problem is that when I run the target build it crashes immediately.
My code is super easy and I don't know what I'm missing.
The DLL is built using CMAKE in a separate project. Here's the code
DLL PROJECT:
CMakeLists
cmake_minimum_required(VERSION 3.5)
project(LibProj LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions("-DBUILD_LIB")
file(GLOB
INCLUDE_FILES
baselibraryclass.h
)
file(GLOB
SOURCE_FILES
baselibraryclass.cpp
)
add_library(yourlib SHARED ${INCLUDE_FILES} ${SOURCE_FILES} )
baselibraryclass.h
#ifndef BASELIBRARYCLASS_H
#define BASELIBRARYCLASS_H
#ifdef BUILD_LIB
#define EXT_DLL __declspec(dllexport)
#else
#define EXT_DLL __declspec(dllimport)
#endif
#include <string>
class EXT_DLL BaseLibraryClass
{
public:
BaseLibraryClass();
};
#endif // BASELIBRARYCLASS_H
baselibraryclass.cpp
#include "baselibraryclass.h"
#include <iostream>
EXT_DLL BaseLibraryClass::BaseLibraryClass()
{
std::cout << "Hi from the library Class Object " << std::endl;
}
Target project
CMakeLists
cmake_minimum_required(VERSION 3.5)
project(TargetProject LANGUAGES CXX)
#Including the path of the library header
include_directories(D:/TestingDLLNativeCpp/Library/include)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(TargetProject main.cpp)
#The path of the built library is D:/TestingDLLNativeCpp/Library/Debug/yourlib.lib
target_link_libraries(TargetProject PRIVATE D:/TestingDLLNativeCpp/Library/Debug/yourlib.lib)
Target project main.cpp
#include <iostream>
#include <baselibraryclass.h>
using namespace std;
int main()
{
BaseLibraryClass testObk;
return 0;
}
As I wrote above, cmake configures properly and the compiler is able to build for both projects, however the target executables crashes immediately.
What am I doing wrong o.O??
Thanks for the attention
The issue of the dll not being included in the build directory might be solved by setting the CMAKE_RUNTIME_OUTPUT_DIRECTORY. However, a better practice would be to set the output directory of the .dll on a target basis. This will ensure that you have no unwanted side-effects in the long run.
set_target_property(yourlib PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}")

Why does Clion not detect standard library headers when using Cmake?

I have a project in Clion using CMake and C++14. The project compiles but all standard library includes are marked as:
"Cannot find string", "Cannot find stdexcept", etc.
Additionally the symbols from the dll I included are not being detected. So they are all marked as:
"Cannot resolve ..."
I've included the header and cmakelist.txt. This is only happening in this project and I have almost identical cmakelist.txt files for all my projects. I have tried restarting CLion's cache. I also tried moving all the files to a new project which worked momentarily but with an hour CLion was flagging these lines again.
cmakelists.txt
cmake_minimum_required(VERSION 3.6)
project(BCI)
set(CMAKE_CXX_STANDARD 14)
#create dlls and executables in the root directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
include_directories(
${CMAKE_SOURCE_DIR}
)
set(SOURCE_FILES
NeuralInterface.hpp
)
add_library(BCI SHARED ${SOURCE_FILES})
set_target_properties(BCI PROPERTIES LINKER_LANGUAGE CXX)
NeuralInterface.hpp
#ifndef NEURALINTERFACE_HPP
#define NEURALINTERFACE_HPP
//c++ includes
#include <stdexcept> //these are the includes which cannnot be resolved
#include <string>
//project includes
#include "okFrontPanelDLL.h"
extern std::string IntanAcquire; //this says cannot resolve container std
...
#endif
What else can I do to CMake so it finds these headers?

C++ testing classes with Catch

I decided to make a small project which I will cover with tests as much as I can. I'm using CLion (which uses CMake) and Catch library for testing.
The problem is that I'm getting undefined reference to TestClass::add method while I run test class.
Here is my setup (it's a dummy one since I wanted to make sure if everything works):
TestClass.h
#ifndef LLL_TESTCLASS_H
#define LLL_TESTCLASS_H
class TestClass {
public:
int add(int a, int b);
};
#endif //LLL_TESTCLASS_H
TestClass.cpp
#include "TestClass.h"
int TestClass::add(int a, int b) {
return a + b;
}
test.cpp -- file with tests
#define CATCH_CONFIG_MAIN
#include "catch.hpp"
#include "../src/TestClass.h"
TEST_CASE("addition") {
TestClass testClass;
REQUIRE(testClass.add(2,3) == 5);
REQUIRE(testClass.add(-1, 1) == 0);
REQUIRE(testClass.add(2, 4) == 1);
}
CMakeLIsts.txt
cmake_minimum_required(VERSION 3.2)
project(LLL)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(LIB_DIR "lib")
include_directories(${LIB_DIR}/Catch)
include_directories(${LIB_DIR}/Eigen)
set(SRC_DIR src)
set(SOURCE_FILES ${SRC_DIR}/main.cpp src/TestClass.h src/TestClass.cpp)
add_executable(LLL ${SOURCE_FILES})
set(TEST_DIR test)
set(TEST_FILES ${TEST_DIR}/test.cpp)
add_executable(LLL_TEST ${TEST_FILES})
You did not specify the TestClass.{.h, .cpp} to be compiled in LLL_TEST executable target:
set(TEST_FILES src/TestClass.h src/TestClass.cpp ${TEST_DIR}/test.cpp)
Or better move it in some shared library and just link to it.
The problem is because your project produces 2 executables: LLL and LLL_TEST targets. Each of them have a object files and a references. In LLL target these references to TestClass are satistifed because they are compiled in the LLL target so the ld can simply link them. But in case of LLL_TEST target compiler can't find these symbols because they are not specified in this target - that's why compiler says that he can't resolve a reference.
Your question has already been answered by Polevoy and he's also correct about factorizing out your class to a library.
I'd just like to show how the library version would look like. You can decide whether it's really so complicated you need to read books or if it has any overhead for doing TDD.
cmake_minimum_required(VERSION 3.2)
project(LLL)
set(CMAKE_CXX_STANDARD 11) # adds -std=c++11
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(LIB_DIR "lib")
include_directories(${LIB_DIR}/Catch)
include_directories(${LIB_DIR}/Eigen)
add_library(LLL_LIB src/TestClass.h src/TestClass.cpp)
add_executable(LLL src/main.cpp)
target_link_libraries(LLL LLL_LIB)
add_executable(LLL_TEST test/test.cpp)
target_link_libraries(LLL_TEST LLL_LIB)
This script creates the static library LLL_LIB which is linked into both executables. You can build a shared one instead if you like:
add_library(LLL_LIB SHARED src/TestClass.h src/TestClass.cpp)
but static is just fine here.