Question

I am trying to group a number of SDL utility classes into a static library for C++ using g++. I am using ubuntu linux 11.10 and gcc versión 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3). The version of ar is 2.21.53.20110810.

Since all files are small I am currently using only header files. I am mentioning this in case it has something to do with the problem. Also, most headers are compiled to object code with -lSDL (I am not sure that is right or even relevant, but it fails either way). Finally, there are a couple of static members for two different classes, defined in their own file to be included in the last steps of the process (triyng to follow the ODR).

Thing is, every file is compiled to object code just fine, then an archive is created using ar but when I try to compile the main file and link it against my new library like this (being libDanGfx.a the archive I am creating):

g++ -Wall -ansi -pedantic newmain.cpp -L. -lSDL -lDanGfx

I get this error:

./libDanGfx.a: could not read symbols: Archive has no index; run ranlib to add one

This happens even after deleting all archives and object code.

I have scouted the site and havent found an answer to the problem yet. Using ranlib did not help at all, also tried feeding the -m32 to my compiler but still to no avail. I tried in three different machines, all of them failed.

Franky, I am thinking of dropping this whole archive thing, update the makefile and just link with the object files but I really want to know what is happening here and learn a bit in the process.

I expect a lot of information to be missing so, just in case, I uploaded a .tar.gz file with the whole thing (just unpack and try the makefile). For your perusing, all .o files will be in the objects directory and every command used can be read in the makefile. Also, the main should display some mindless stuff on the screen (probably red squares since the resource files are not uploaded to save space, of course, .o and .a files are excluded as well).

The file can be reached in this link.

Was it helpful?

Solution

Reading your question again and checking the archive, you only have the code in header files? But then you can't create a library! Header files are for including in source files, and not to be compiled into object files!

Split the headers up into proper header files with only declarations, and put definitions (i.e. the implementation) in source files. Compile the source files into object files that are used to create the library.

If there is no "implementation" and all code is inline in the header files, you don't need to make a library, just include the wanted header file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top