G-71
G-71

Reputation: 3682

Using created DLL-file (call function from application)

I have got a dummy question. I have three C files: f1.c f2.c f3.c, which contain:

// f1.c
int f1()
{
    return 2;
}

// f2.c
int f2()
{
    return 4;
}

// f3.c
int f3()
{
    return 10;
}

I have already 3 object file, when i run the following command (i use mingc under windows7):

gcc -c f1.c f2.c f3.c

and i create dll file:

gcc f1.o f2.o f3.o -o test1.dll -shared

using DLL Export Viewer i have opened this file: enter image description here

How I can use this file in my application (crossplatform)? How I can call functrion f1, f2, f3 ?

Sorry for my bad English

Upvotes: 2

Views: 165

Answers (2)

Daniel Trebbien
Daniel Trebbien

Reputation: 39208

Assuming you have the header, the only thing that you are missing is the A archive, which the linker needs in order to figure out what a DLL provides.

Change this:

gcc f1.o f2.o f3.o -o test1.dll -shared

To this:

gcc f1.o f2.o f3.o -o test1.dll -shared -Wl,--out-implib,libtest1.a

Then, to link with the shared library, pass -ltest1 to gcc.

You will still need to recompile the library for each platform and architecture (x86, AMD64, IA64, etc.). Windows uses DLLs, but Linux uses Shared Objects, for example.

See http://www.mingw.org/wiki/sampleDLL for more information.

Upvotes: 2

David Heffernan
David Heffernan

Reputation: 612794

  1. Include the header file in the calling program.
  2. Link the calling program to the .lib file associated with the library.
  3. Make sure that the library is in the library search path.

Upvotes: 1

Related Questions