Using a dynamic lib

How can i link Dynamic libs to my project?

I created a dynamic lib (hello.dll) compiled from the file:
1
2
3
4
5
int hello() {

	1;

}


and then i tried to link it to an object file (test.o) from the file:
1
2
3
4
5
6
int main() {

	hello();
	return 0;

}

using the command:
gcc -o test test.o -lhello
but it didnt work, it tells me -lhello could not be found.

Thanks in advance
Last edited on
I'm not using gcc nor linux at the time but AFIK you have to create *DEF file inside DLL to be able importing your functions.

do you know how to create definition file?
Here is an example on how to export names:

HEADER FILE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#ifndef DLL_H
#define DLL_H

// MathFuncsDll.h

namespace math {

	class MyMathFuncs
	{
	public:
		// Returns a + b
		static double CALL Add(double a, double b);

		// Returns a - b
		static double CALL Subtract(double a, double b);

		// Returns a * b
		static double CALL Multiply(double a, double b);

		// Returns a / b
		// Throws DivideByZeroException if b is 0
		static double CALL Divide(double a, double b);
	};
}
#endif 


CPP FILE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "Dll.h"
#include <stdexcept>
#include <iostream>
using namespace std;

namespace math {

    double MyMathFuncs::Add(double a, double b)
    {
        return a + b;
    }

    double MyMathFuncs::Subtract(double a, double b)
    {
        return a - b;
    }

    double MyMathFuncs::Multiply(double a, double b)
    {
        return a * b;
    }

    double MyMathFuncs::Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }
}



DEFINITION FILE:
1
2
3
4
5
6
7
8
9
LIBRARY "Dll"

EXPORTS

	Add = ?Add@MyMathFuncs@math@@SGNNN@Z
	Divide = ?Divide@MyMathFuncs@math@@SGNNN@Z
	Multiply = ?Multiply@MyMathFuncs@math@@SGNNN@Z
	Subtract = ?Subtract@MyMathFuncs@math@@SGNNN@Z
	f = ?f@math@@YGXXZ


you'll have to use some tool to find out mangle names, I don't know how do do that on linux.
when you find out mangle name the you assign your funciton names to that mangled names like you see in my example.

EDIT:
after you do that then just compile the project with all the files including definition file.
at the end you got a dll which exports those funcitons in human readable names.

you'll have to link that dll to your IDE to be able using funcitons...
you'll have to load the library into your project... also I have no idea on how to do that on linux.

Hope you got it :D
Last edited on
Are there any good articles on the subject of .def creation?

I tried to google it, but i didnt find much on the subject since visual c++ creates the import lib automatically
Topic archived. No new replies allowed.