Sign in to follow this  

Static class function

Recommended Posts

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.

Share this post

Link to post
Share on other sites
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:
collect2: ld returned 1 exit status
36:~ panda$ g++ --version
powerpc-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 NO

36:~ panda$

I have to wonder why this hotel is using 36 as a hostname <_< (edit: or not: it's an 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).

Share this post

Link to post
Share on other sites
"undefined reference" is actually the error message.

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

g++ <compile flags> <link flags (e.g. -lA)> <source files> -o <outputfile>

When I put the link after the source files:

g++ <compile flags> <source files> <link flags (e.g. -lA)> -o <outputfile>

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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this