开发者

g++ static library depends on dynamic libraries

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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜