views:

63

answers:

1

I have some static library. for example libpuchuu.a it depends on dynamic library. for example libSDL.so (but of cource I have libSDL.a) Creation of libpuchuu.a is simple:

ar -rcs object_file_1.o object_file_2.o

But I can't link my project with libpuchuu.a! undefined references attack my console!

At some forum I have found such sentence: all dependences of static library is in this library if your object files depends on y.a you must unpack(ar) y.a object files to your folder, then pack all objects to your new library. Questions:

  1. is it true?
  2. if it is true: object file puchuu.o depends on libSDL.a how to make libpuchuu.a?
  3. another solution?

PS

I have made a simple example and posted it to pastebin:

http://paste.lisp.org/display/115456

building commands:

g++ -O3 -c index.cpp -I/usr/local/include/SDL
g++ -O3 -c lib.cpp -I/usr/local/include/SDL
ar rv libpuchuu.a lib.o
g++ -O3 -o program.exe index.o -L/usr/local/lib -lmingw32 -lSDLmain -lSDL -mwindows -lSDL_image -L. -lpuchuu

... undefined references from index.cpp!

+1  A: 

Have you tried adding -lSDL to the command line of your linker?

The undefined references are to symbols you are trying to define in lib.cpp which you use ar to package up in libpuchuu.a. Unfortunately, you aren't defining the symbols you think you're defining. using namespace sdl; does not automagically cause everything you define to be in the sdl namespace. All it does is arrange it so that when the compiler looks for a particular name it looks for it in the sdl namespace along with the main namespace.

You need to wrap the functions defined in lib.cpp with namespace sdl { .... functions ... } and your code will link just fine.

In my experience, when I make a mistake with something unfamiliar, the mistake is usually a really dumb one that has nothing to do with the thing I'm unfamiliar with. But because I'm working in unknown territory I automatically assume that my lack of knowledge of the territory must be at fault, not that I did something stupid.

You might find that experience of mine applicable.

And, as a side note, the idea of 'undefined references attacking your console' is rather amusingly bizarre, and leaves me with an image of your screen and keyboard left in a smoking ruin after you try to link.

Omnifarious
of cource. but undefined references attack!
puchu
@puchu - Adding `-lSDL` should've solved your problem. If you provide more detail about the exact commands you're running, perhaps the actual problem will be uncovered, cowering in its hiding place.
Omnifarious
>>undefined references are to symbols you define in lib.cpp. as you see answer is how to fix it. just write...
puchu
@puchu - I did.
Omnifarious
oh... there are many english words I have never seen... I am translating...
puchu
ok! namespace sdl { in lib.cpp... now checking...
puchu
it works!!!!!!!!
puchu