creating and loading dll files
Answered
im trying to create my own dll file with clion on windows and mingw, so far this is my code
Math.h
#ifdef MATH_EXPORTS
#define MATH_API __declspec(dllexport)
#else
#define MATH_API __declspec(dllimport)
#endif
class MATH_API Math {
static double add(double a, double b);
static double mul(double a, doulbe b);
}
Math.cpp
#include "Math.h"
double Math::Math add(double a, double b) {
return a + b;
}
double Math::Math mul(double a, double b) {
return a * b;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(Math)
set(CMAKE_CXX_STANDARD 14)
add_library(Math SHARED Math.h Math.cpp)
thats creating a file and dll file.
I found it very hard to use it no matter what im doing there is an error of not founding -lMathLib
I opened a new project, created new directory named bin and put there Math.h libMathLib.a and libMathLib.dll and that what I did:
main.cpp
#include <iostream>
#include "Math.h"
int main() {
std::cout << Math::add(3, 5) << std::endl;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.13)
project(MathTest)
set(CMAKE_CXX_STANDARD 14)
include_directories(${CMAKE_SOURCE_DIR}/bin)
add_executable(MathTest main.cpp)
target_link_libraries(MathTest bin/libMathLib.dll)
please help Im getting crazy for the last two days nothing on the net to help me out
Please sign in to leave a comment.
Hi! Please see this SO thread, it looks related.
hi,
thank you for your response.
as you can see in my code I did excatly the same thing except I wrote the exports manually.
the problem is for some reason with .a file its working but with dll file its not.
when I put the dll file in same directory with the exe of my program, the program was ok.
If I will put the dll file in somewhere else its stop working.
But If I use .a file I can put it wherever I like and it works
I did the following:
1) The first project
math.h
math.cpp
CMakeLists.txt
2) The second project
I built the Math library in the first project, created the bin subfolder in the second project, copied libMath.dll and math.h into this subfolder.
main.cpp
CMakeLists.txt
The MathTest executable was built correctly:
I know it will be build but try to run the main, you suppose to see 8 on the screen no?
this is what I see, and I did from scratch exactly like you
C:\Users\asafanter\Desktop\ClionTest\MathTest\cmake-build-debug\MathTest.exe
Process finished with exit code -1073741515 (0xC0000135)
On Windows the corresponding dll needs to be in the same folder as the exe.
From Wikipedia, for example:
Unless your DLL is a Component Object Model (COM) server, the
DLLfile must be placed in one of the directories listed in the PATH environment variable, in the default system directory, or in the same directory as the program using it.ok but you put it in "bin" subfolder, so it can't work?
do I have to put all my dll files in same directory with the exe on windows?
>ok but you put it in "bin" subfolder, so it can't work?
Yes, in this case if I run the .exe file, it can't find the .dll file. I misunderstood you previously, sorry for that.
>do I have to put all my dll files in same directory with the exe on windows?
All .dll files which you use in your project.
Please note that all these questions are general ones, not related to CLion.
oh Im sorry, you help me a lot
thank you very much :)