miniupnp.tuxfamily.org Forum Index miniupnp.tuxfamily.org
The forum about miniupnp and libnatpmp
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Compile program with MinGW

 
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> libnatpmp Compilation/Installation
View previous topic :: View next topic  
Author Message
sidarape



Joined: 30 Dec 2010
Posts: 5

PostPosted: Thu Dec 30, 2010 10:25 pm    Post subject: Compile program with MinGW Reply with quote

Hello,
First, I want to advertise you that english is not my first language and he is not very good.

So, the subject of this topic: I downloaded and installed MinGW for Windows and I compile libnatpmp with gcc. That worked very fine. Next, I put the headers in C:\MinGW\include and the natpmp.a generated by gcc in C:\MinGW\lib. I take the DLL and put it into the future executable of my program. So, I tried to compile a program with libnatpmp.h include but the compilation of program crash: There is the exit of gcc:
Quote:
C:\Users\Fredy\AppData\Local\Temp\ccgnf3bs.o:main.c:(.text+0x45): référence indéfinie vers « _imp__initnatpmp »
C:\Users\Fredy\AppData\Local\Temp\ccgnf3bs.o:main.c:(.text+0x78): référence indéfinie vers « _imp__sendnewportmappingrequest »
C:\Users\Fredy\AppData\Local\Temp\ccgnf3bs.o:main.c:(.text+0xf0): référence indéfinie vers « _imp__getnatpmprequesttimeout »
C:\Users\Fredy\AppData\Local\Temp\ccgnf3bs.o:main.c:(.text+0x11f): référence indéfinie vers « select@20 »
C:\Users\Fredy\AppData\Local\Temp\ccgnf3bs.o:main.c:(.text+0x134): référence indéfinie vers « _imp__readnatpmpresponseorretry »
C:\Users\Fredy\AppData\Local\Temp\ccgnf3bs.o:main.c:(.text+0x176): référence indéfinie vers « _imp__closenatpmp »
collect2: ld returned 1 exit status

The exit is half in french: "référence indéfinie vers" means "undefined reference to".

The code of the program that I tried to compile is:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <natpmp.h>

int main()
{
   printf("Compilation reussie");
   return 0;
}

void redirect(uint16_t privateport, uint16_t publicport)
{
  int r;
  natpmp_t natpmp;
  natpmpresp_t response;
  initnatpmp(&natpmp);
  sendnewportmappingrequest(&natpmp, NATPMP_PROTOCOL_TCP, privateport, publicport, 3600);
  do {
    fd_set fds;
    struct timeval timeout;
    FD_ZERO(&fds);
    FD_SET(natpmp.s, &fds);
    getnatpmprequesttimeout(&natpmp, &timeout);
    select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
    r = readnatpmpresponseorretry(&natpmp, &response);
  } while(r==NATPMP_TRYAGAIN);
  printf("mapped public port %hu to localport %hu liftime %u\n",
         response.pnu.newportmapping.mappedpublicport,
         response.pnu.newportmapping.privateport,
         response.pnu.newportmapping.lifetime);
  closenatpmp(&natpmp);
}

It is the code of the first example of this site except for "response.pnu." that was only "response." because the compilation of this crashed.

Thanks to all of you for your answers.
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1589

PostPosted: Fri Dec 31, 2010 10:43 am    Post subject: Reply with quote

you shoud define the STATICLIB macro in your code if you are linking against the static library (.a) and not the .lib for .dll linking.
_________________
Main miniUPnP author.
https://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
sidarape



Joined: 30 Dec 2010
Posts: 5

PostPosted: Fri Dec 31, 2010 5:41 pm    Post subject: Reply with quote

miniupnp wrote:
you shoud define the STATICLIB macro in your code if you are linking against the static library (.a) and not the .lib for .dll linking.

What is the content of this macro is supposed to have?
Back to top
View user's profile Send private message
sidarape



Joined: 30 Dec 2010
Posts: 5

PostPosted: Fri Dec 31, 2010 8:56 pm    Post subject: Reply with quote

Finally, I was able to compile my program by generating a new .a file with this command:
Code:
dlltool --input-def natpmp.dll.def --dllname natpmp.dll --output-lib libnatpmp.a -k

Next, I put the result .a in the lib directory of MinGW. I was important that the name begin by "lib". And there is the command line to compile my program:
Code:
gcc main.c -o nat -lws2_32 -lnatpmp

Do you know why I had to do this? If no, I think that it would important to mention the proceed of compilation in the documentation.
Thank you for your answer.
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1589

PostPosted: Sat Jan 01, 2011 6:29 pm    Post subject: Reply with quote

well, the Makefile for mingw32 does produce both libminiupnpc.a (static library) and miniupnpc.lib/miniupnpc.dll (dynamic linking library).
You can then choose to link your program against one of theses.
You can take example on how upnpc-static.exe and upnpc-shared.exe are build.
_________________
Main miniUPnP author.
https://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
sidarape



Joined: 30 Dec 2010
Posts: 5

PostPosted: Sat Jan 01, 2011 7:57 pm    Post subject: Reply with quote

Ok thanks, I didn't think that gcc could take .lib library. There is the final command that I use for people who would visit this forum:
Code:
g++ main.cpp natpmp.lib -o nat -lws2_32


So, I have another little problem and I think that it is not necessary to open another topic.

I tried this program from the original example from this site but I managed the errors:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <natpmp.h>

bool redirect(uint16_t privateport, uint16_t publicport);

int main()
{
   unsigned short port = 80;
   if(!redirect(port, port))
   {
      printf("Redirection refuse\r\n");
   }

   return 0;
}

bool redirect(uint16_t privateport, uint16_t publicport)
{
   bool valide = false;
   int erreur = 0;
   
   int r;
   natpmp_t natpmp;
   natpmpresp_t response;
   if((erreur = initnatpmp(&natpmp)) == 0)
   {
      if((erreur = sendnewportmappingrequest(&natpmp, NATPMP_PROTOCOL_TCP, privateport, publicport, 3600)) == 12)
      {
         do {
            fd_set fds;
            struct timeval timeout;
            FD_ZERO(&fds);
            FD_SET(natpmp.s, &fds);
            getnatpmprequesttimeout(&natpmp, &timeout);
            select(FD_SETSIZE, &fds, NULL, NULL, &timeout);
            r = readnatpmpresponseorretry(&natpmp, &response);
         } while(r==NATPMP_TRYAGAIN);
         if((erreur = r) == 0)
         {
            printf("mapped public port %hu to localport %hu liftime %u\n",
                response.pnu.newportmapping.mappedpublicport,
                response.pnu.newportmapping.privateport,
                response.pnu.newportmapping.lifetime);
            if((erreur = closenatpmp(&natpmp)) == 0)
            {
               valide = true;
            }
         }
      }
   }
   else
   printf("Erreur: %i\n%i\n", erreur, WSAGetLastError());
   
   return valide;
}

The exit of this program is:
Quote:
Erreur: -2
10093
Redirection refuse

I see in the code where the error come from, there are the lines:
Code:
   p->s = socket(PF_INET, SOCK_DGRAM, 0);
   if(p->s < 0)
      return NATPMP_ERR_SOCKETERROR;

I really don't see why the creation of the socket crash. I tried to run my code as an administrator and without firewall. Do you know why this doesn't work and how to get the socket error type?
Back to top
View user's profile Send private message
miniupnp
Site Admin


Joined: 14 Apr 2007
Posts: 1589

PostPosted: Sat Jan 01, 2011 8:37 pm    Post subject: Reply with quote

look at the sample program natpmpc.c or upnpc.c and add the code
Code:

#ifdef WIN32
   WSADATA wsaData;
   int nResult = WSAStartup(MAKEWORD(2,2), &wsaData);
   if(nResult != NO_ERROR)
   {
      fprintf(stderr, "WSAStartup() failed.\n");
      return -1;
   }
#endif

at the begining of your main() function.
_________________
Main miniUPnP author.
https://miniupnp.tuxfamily.org/
Back to top
View user's profile Send private message Visit poster's website
sidarape



Joined: 30 Dec 2010
Posts: 5

PostPosted: Sat Jan 01, 2011 11:44 pm    Post subject: Reply with quote

Thanks to you, I think that's all of my questions for the moment. If I have more question, I will be back here.

Also, I would like to say congratulation for your program and thanks for the participation of the FS community.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    miniupnp.tuxfamily.org Forum Index -> libnatpmp Compilation/Installation All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP
© 2007 Thomas Bernard, author of MiniUPNP.