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
 
0
9 comments

Hi! Please see this SO thread, it looks related.

0
Avatar
Permanently deleted user

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

0

I did the following:

1) The first project 

math.h

#define MATH_EXPORTS

#ifdef MATH_EXPORTS
#define MATH_API __declspec(dllexport)
#else
#define MATH_API __declspec(dllimport)
#endif

class MATH_API Math {
public:
static double add(double a, double b);
static double mul(double a, double b);
};

math.cpp

#include "math.h"

double Math::add(double a, double b) {
return a + b;
}

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

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

#include <iostream>
#include "bin/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)

add_executable(MathTest main.cpp)

target_link_libraries(MathTest ${CMAKE_SOURCE_DIR}/bin/libMath.dll)

The MathTest executable was built correctly:

0
Avatar
Permanently deleted user

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)

0

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 DLL file 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.

0
Avatar
Permanently deleted user

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?

0

>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.

0
Avatar
Permanently deleted user

oh Im sorry, you help me a lot

thank you very much :)

0
Avatar
Permanently deleted user
cmake_minimum_required(VERSION 3.13)
project(Math)

set(CMAKE_CXX_STANDARD 14)

add_library(Math SHARED math.h math.cpp)
Then it shows how to use the DLL from another C++ app. DLLs (also known as shared libraries in UNIX-based operating systems) are one of the most useful kinds of Windows components. You can use them in as fall guys as a way to share code and resources, and to shrink the size of your apps. DLLs can even make it easier to service and extend your apps.
0

Please sign in to leave a comment.