Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Linking my DLL with executable file

shared library dll windows api

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 29 July 2015 - 01:07 PM

Hi guys,for anyone reason i decide to make a simple DLL and linking with another executable just for know how do this things.
So this is the code of DLL.
I know that this code is not according with MSDN especifications but it's work:

 

int somar(int x,int y){

return x + y;
}

int subtrair(int x,int y){

return x + y;
}

int mutiplicar(int x,int y){

return x + y;
}

int dividir(int x,int y){

return x / y;
}

Then i wrote this simple code for test my dll.

#include <stdio.h>

int main(int argc,char *argv){

	printf("%d",soma(10,20));

	return 1;
	
}

If i use cl compiler from microsoft i get error that the symbol is not find if i compile with gcc i get the same.

The only way that i found to use this function from my dll is using LoadLibrary and GetProcAddress but i dont want this way.

Anyone can help me?

 



#2 0xDEADBEEF

0xDEADBEEF

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 790 posts

Posted 29 July 2015 - 02:20 PM

If you import the lib into your project you can use a dll like a static library.


Creating SEGFAULTs since 1995.


#3 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 29 July 2015 - 05:48 PM

How i do that?



#4 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 30 July 2015 - 10:43 AM

Sorry,but the question is above has incomplete,the correct is: How i do that in comand line?



#5 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 30 July 2015 - 12:16 PM

You don't. You do it in code.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#6 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 31 July 2015 - 01:18 PM

But how,how my dear friend....?



#7 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 02 August 2015 - 07:48 AM

What compiler are you using, to start with? Here are some instructions for mingw: http://stackoverflow...another-program


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#8 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 02 August 2015 - 07:32 PM

I am using microsoft compiler cl.
When i do some things with assembly i linking the obj file with golink and it's simple link to a specific DLL.
But isn't working this time...


Edited by FrancoAti, 02 August 2015 - 07:32 PM.


#9 Gikoskos

Gikoskos

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 04 August 2015 - 12:04 AM

I am using microsoft compiler cl.
When i do some things with assembly i linking the obj file with golink and it's simple link to a specific DLL.
But isn't working this time...

 

To be able to use the dll you have to include a header in your main program that has declarations of the various functions you can use from the API of the library. For example:
Let's say your main program is main.c :
 

#include <stdio.h>

int main()
{
      print_result(add_numbers(2,3);
      return 0;
}

And your library source code is add.c :
 

#include <stdio.h>

int add_numbers(int,int);
void print_result(int);

int add_numbers(int x, int y)
{
      return x+y;
}

void print_result(int z)
{
      printf("Result is: %d\n", z);
}

To link add.c to main.c you have to create a header file and put your add.c function prototypes there, for example add.h :

#ifndef ADD_H
#define ADD_H

int add_numbers(int, int);
void print_result(int);

#endif

Then you include your header file to the add.c library file like so (also remove the prototypes from your add.c) :
 

#include <stdio.h>
#include "add.h"  /*Given that add.h is in the same directory as add.c.
                   * If it's not, you have to specify the folder it's in (e.g. #include "lib/add.h") */

int add_numbers(int x, int y)
{
      return x+y;
}

void print_result(int z)
{
      printf("Result is: %d\n", z);
}

You then compile your add.c file to an object file (e.g. gcc -c add.c) and you turn it into a .dll with the steps you already know (I assume). Then you can use the functions from the add.dll file in your main.c like so:

#include <stdio.h>
#include "add.h"

int main()
{
      print_result(add_numbers(2,3);
      return 0;
}

And it works!
Careful: you need the header in order to gain access to the dll. Without it you can't do anything. After you compile main.c though you don't need it to run the program, you only need the add.dll to be in the same directory as the executable.
Hope that helped!


Edited by Gikoskos, 04 August 2015 - 12:15 AM.


#10 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 04 August 2015 - 08:17 PM

The only way that i find to load the DLL is using LoadLibrary and GetProcAddress.
thanks man, i will test this way...
 



#11 Gikoskos

Gikoskos

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 05 August 2015 - 02:14 AM

The only way that i find to load the DLL is using LoadLibrary and GetProcAddress.
thanks man, i will test this way...
 

Don't forget to link your library with your executable, otherwise it won't compile!
For example if you're using gcc and your dll file is in the folder /test/test.dll then you compile it like this:               gcc -g main.c -L/test/ -ltest



#12 FrancoAti

FrancoAti

    CC Regular

  • Member
  • PipPipPip
  • 32 posts

Posted 05 August 2015 - 10:38 AM

thanks for the plus.
But i'am using CL microsoft compiler and dont GCC.


Edited by FrancoAti, 05 August 2015 - 10:39 AM.





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download