Circular static libraries

I get "undefined reference" errors when linking an executable from a compiled object and a slew of static libraries that have many interdependencies:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
obj/CoolSim.o: In function `main':
CoolSim.cc:(.text+0x48): undefined reference to `G4UIterminal::G4UIterminal(G4VUIshell*, bool)'
CoolSim.cc:(.text+0xa6): undefined reference to `CSRunManager::CSRunManager(G4String)'
CoolSim.cc:(.text+0xed): undefined reference to `CSDetectorConstruction::CSDetectorConstruction(G4String)'
CoolSim.cc:(.text+0x124): undefined reference to `CSPhysicsList::CSPhysicsList()'
CoolSim.cc:(.text+0x13c): undefined reference to `G4RunManagerKernel::SetPhysics(G4VUserPhysicsList*)'
.
.
.
etc
.
.
.
obj/CoolSim.o: In function `G4VTouchable::GetVolume(int) const':
CoolSim.cc:(.gnu.linkonce.t._ZNK12G4VTouchable9GetVolumeEi+0x36): undefined reference to `G4Exception(char const*, char const*, G4ExceptionSeverity, char const*)'
obj/CoolSim.o: In function `G4VTouchable::GetSolid(int) const':
CoolSim.cc:(.gnu.linkonce.t._ZNK12G4VTouchable8GetSolidEi+0x36): undefined reference to `G4Exception(char const*, char const*, G4ExceptionSeverity, char const*)'
obj/CoolSim.o: In function `G4VTouchable::GetReplicaNumber(int) const':
CoolSim.cc:(.gnu.linkonce.t._ZNK12G4VTouchable16GetReplicaNumberEi+0x36): undefined reference to `G4Exception(char const*, char const*, G4ExceptionSeverity, char const*)'
.
.
.
etc


However, I'm very sure all header files were included when compiling the object obj/CoolSim.o, that all the static libraries were archived correctly and contain all the objects they should, which were all compiled correctly, and that the all the libraries were included correctly in the linking of the final binary.

Compilation failed on a Linux system (gcc 3.4.6), but worked fine on a Mac (gcc 4.0.1) with a nearly identical setup. A little research on the internet indicated that it might be a problem with circular libraries and the order in which I'm including the libraries.

It's driving me nuts. Any help is much appreciated.
Last edited on
these functions are not found in the libraries with which you are trying to link.
like the definitation of:
G4RunManagerKernel::SetPhysics(G4VUserPhysicsList*)

is not found in the linked libraries. Either you are linking with unsupported version or an old version which dont have these functions defined or not giving the library name.

try using -l option and setting the lib path using -L option.
That can't be it --- that function is most definitely in the G4RunManagerKernel.hh/cc files, and the object file for that class is definitely in a library that is include with -l and is in a directoy included with -L. Again, this all compiles perfectly fine on my Mac, where I have exactly the same installation of the G4 libraries (a particle physics suite) which is giving that error. I think it has to do with the handling of the order in which static libraries are included. The linux compilation is being done with gcc 3.4.6, and the mac with 4.0.1 ... was there some large change made with ordering that allows my Mac to compile but not the linux box?
That's strange. I use Linux and I know that gcc on Linux links the libraries in the order they are specified to the linker (ie, the order specified on the command line).

In the case where you have circular dependencies (foo.a calls functions in bar.a and vice versa) the way around this is to specify one of the libraries twice on the link line, eg

g++ -o myapp -lfoo -lbar -lfoo *.o

What I don't understand is why the error is coming from my main program. If it's a circular static libraries problem, shouldn't I get an error that says these functions aren't defined in the libraries that are calling them? My main program object naturally comes after all the libraries in the linking command.
i think there is some library compatibility issue.. if you have a common compilation for mac and Linux then this must be causing it. do a ldd on mac binary. it will give you the library dependency list.

now you can try finding these functions in the same libraries on linux using string <library_name>
they probably are missing and might be defined in some other library..

it is just an advice.. i am not sure.
It's not a common compilation. Common source, yes, but compiled for each system natively. I'm very sure that all the functions are in the libraries. I had compiled this exact same code into one large static library, and then there was no problem. When I broke up different sections of the code into separate libraries, that's where the trouble began.
my suggestions exhausted.. :(
no comments..!!
Can you post the compilation line here? I guess that you have the incorrect order of libraries mentioned in the link line. Your application specific libraries (G4) should be at the end followed by system libraries such as -lm etc.

Ideally, it should look like g++ <options> <file name> <your app libs> <system libs>

obj/CoolSim.o: In function `G4VTouchable::GetVolume(int) const':
CoolSim.cc:(.gnu.linkonce.t._ZNK12G4VTouchable9GetVolumeEi+0x36): undefined reference to `G4Exception(char const*, char const*, G4ExceptionSeverity, char const*)'

The above one indicates that the library containing G4Exception is not found during the linking of G4VTouchable. So keep the library of G4Exception before the library containing G4VTouchable in the link line. Give it a try... I am sure you can resolve this by rearranging the -l and -L in the link line because you are able to link them when all the functions are in single library. All the best!!
I think this is exactly the problem. I'd post the compilation line, but it's terribly long and I don't think it would shed any light. I discovered that somewhere along the way I was just including the G4 libraries in alphabetic order, which is probably causing some of the problems. I'm rectifying this, and hopefully it will work.

I also discovered why it works without problem on a Mac. The Darwin g++ compiler has a switch -bind_at_load which allows it to call the included libraries in the order it needs regardless of the order they are listed in on the compilation line. Is there something similar to this in the Linux g++?
Topic archived. No new replies allowed.