Jump to content

Undefined reference

- - - - -

  • Please log in to reply
12 replies to this topic

#1
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
So, I'm working on my own mini library (wrapper mostly) and got some issues. Among others, I have Socket and Timer class. And since I'm a bit lazy I'm not working on portability as seperate files but like this:

void Timer::Sleep(int milisec) {

#ifdef _WIN32

    Sleep(milisec);

#else

    usleep(milisec * 1000);

#endif

}

Code compiles, but when I run the program it crashes and I've no idea why. Next problem I have is with Socket class; again the code compiles but when I try to run it I get undefined reference errors to Win API functions like undefined reference to `_WSAStartup@8', undefined reference to `_socket@12' and so on.

I'm following Static Libraries: (.a) method found here

Any help would be much appreciated.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
What are your compiler flags?
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
You're saying that it's barfing on Win32 functions when linking on a NIX system? On a whim, try
#if defined(_MSC_VER) && defined(_WIN32)
sudo rm -rf /

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
@Nullw0rm, I'm using
g++ -Wall -c socket.cpp timer.cpp
and then
ar -cvq lib.a socket.o timer.o

@dargueta, I'm on Windows, but trying to make it somewhat portable should I need it on a unix systems.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Oh wow...I just remembered I had this problem before and completely forgot the solution (which I just remembered). You need to link either winsock.lib or ws32_2.lib depending on what version of Windows and/or Visual Studio you're running. I'm assuming you're using VS, right? I don't know the way to link right now because the stuff I've found is really convoluted. Then again, that's Microsoft for ya. There's probably a "References" dialog in the Project Properties section, so add winsock.lib first and try compiling with that. If that doesn't work then try ws32_2.lib. Do not use both.

With g++ add the -lsocket -lnls options wherever you like.
sudo rm -rf /

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I'm using Code::Blocks and have -lwsock32 flag under linker options but I guess I should use it when I'm compiling lib, right?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Compiling any part of your project.
sudo rm -rf /

#8
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I'm not good at manual compiling, but when I tried
g++ -Wall -c timer.cpp socket.cpp -l wsock32
I got g++: -lwsock32: linker input file unused because linking not done error. Am I doing this right? I forgot to mention earlier, I have code, for sockets, which uses Win API and it works fine; the only difference is that here I don't use #ifdef _WIN32 directives. Both files are not under any project and one where I don't use preprocessor directives, works.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#9
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Are you using g++ on Windows?
sudo rm -rf /

#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1

dargueta said:

Are you using g++ on Windows?
MinGW 5.1.6, shouldn't I?
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
If you're using g++ on Unix, that shouldn't link at all. You need to use

g++ -lsocket -lnls <other options and stuff here>

Those are lowercase L's by the way.
sudo rm -rf /

#12
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
Now it's just *.hpp files, no lib's, and it works fine, even with preprocessor directives. Anyways, thanks for help.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users