Skip to main content
gamedev.net
🔒 Locked

Static class function

Started by Simplicity May 17, 2006 at 3:03 PM 4 replies 650+ views
Original Post
Simplicity
Simplicity
Why does the linker in Linux give "undefined reference" for static class function declared in an external library? libA.a: class SomeClass { public: static void StaticFunc(); }; void SomeClass::StaticFunc() {} exe: int main() { SomeClass::StaticFunc(); } "undefined reference to 'SomeClass:StaticFunc' Even if I put the class under some namespace, them same problem occurs. I'm using g++ to compile both by the way.
Fruny
Fruny
Because you haven't declared the function?
Because you haven't linked the library?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Deyja
Deyja
I'd bet money that that isn't the actual error. It's /unresolved/ reference, isn't it?
MaulingMonkey
MaulingMonkey
Quote:
Original post by Deyja
I'd bet money that that isn't the actual error. It's /unresolved/ reference, isn't it?


And I'd likely take your money. Well maybe. Here's the output on OS X:

36:~ panda$ g++ foo.cpp -o foo/usr/bin/ld: Undefined symbols:example()collect2: ld returned 1 exit status36:~ panda$ g++ --versionpowerpc-apple-darwin8-g++-4.0.0 (GCC) 4.0.0 20041026 (Apple Computer, Inc. build 4061)Copyright (C) 2004 Free Software Foundation, Inc.This is free software; see the source for copying conditions.  There is NOwarranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.36:~ panda$ 


I have to wonder why this hotel is using 36 as a hostname <_< (edit: or not: it's an .in-addr.arpa hostname, the shell just prints out the first digits, durr). Bold and underlining done by me of course.


Simplicity: Try -lA. If that dosn't work, chances are you've borked building your library (or you're linking against the wrong one).
Deyja
Deyja
That's an undefined symbol, not an undefined reference. Symbols are defined; References are resolved. :P
Simplicity
Simplicity
"undefined reference" is actually the error message.

I've found the solution. Turns out, what I had was:

g++ -o

When I put the link after the source files:

g++ -o

everything is ok. It is really weird that other functions are ok either way, but static functions you need to put the "link flags" after specifying the source files.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.