views:

782

answers:

2

I'm having several "undefined reference" (during linkage) and "unresolved symbol" (during runtime after dlopen) issues where I work. It is quite a large makefile system.

Are there general rules and guidelines for linking libraries and using compiler flags/options to evade these types of errors?

+2  A: 

Setting up a build system where X depends on Y which depends on Z helps. It's when you get into circles (Z depends on X) that things get ugly.

Oftentimes it's the order libraries are linked ("-lZ -lY -lX" vs "-lX -lY -lZ") that causes grief. More rarely, you have the same library-name in multiple places on your search path, or your linking against outdated versions that have not yet been recompiled.

"nm --demangle" can let you see where things are defined/used.

"ldd" can be used to see what dynamic libraries you depend on.

The gcc/g++ flag -print-file-name=LIBRARY can help track down exactly which library is being used.


Afterthought: (Since you ask about rules/guidelines.)

It is possible to set up a makefile system such that:

  • If module=D depends on modules A,B,&C.
  • Then trying to make module=D would first make modules A,B,&C.
  • And, more importantly, module=D would automatically determine its libraries (-lA,etc), library paths (-LA), and include paths (-IA) from the makefiles for modules A,B,&C.

That can get a little hairy to set up. Last time I did it, I favored merely caching the information rather than forking an excessive number of make subprocesses. Coupled with makefile-importing and a little perl script to remove duplicates. Kludgey, I know. (Powers that be didn't want to spend time on infrastructure.) But it can be done.

Then again, I was using GNU-make, which has a few extensions.

Mr.Ree
+2  A: 

IF YOU WERE USING MSVC :

You cannot evade this type of error by setting a flag : it means some units (.cpp) dont' have definitions of declared identifiers. It's certainly caused by missing includes or missing object definitions (often static objects) somewhere.

While developing you can follow those guidelines ( from those articles ) to be sure all your cpp includes all the headers they need but no more :

  • Every cpp file includes its own header file first. This is the most important guideline; everything else follows from here. The only exception to this rule are precompiled header includes in Visual Studio; those always have to be the first include in the file. More about precompiled headers in part two of this article.
  • A header file must include all the header files necessary to parse it. This goes hand in hand with the first guideline. I know some people try to never include header files within header files claiming efficiency or something along those lines. However, if a file must be included before a header file can be parsed, it has to be included somewhere. The advantage of including it directly in the header file is that we can always decide to pull in a header file we’re interested in and we’re guaranteed that it’ll work as is. We don’t have to play the “guess what other headers you need” game.
  • A header file should have the bare minimum number of header files necessary to parse it. The previous rule said you should have all the includes you need in a header file. This rule says you shouldn’t have any more than you have to. Clearly, start by removing (or not adding in the first place) useless include statements. Then, use as many forward declarations as you can instead of includes. If all you have are references or pointers to a class, you don’t need to include that class’ header file; a forward reference will do nicely and much more efficiently.

But as commenter have suggested, it seem you're using g++...

Klaim
From the problem stated, he has link-ordering issues. It's not a .ccp->.o issue. It's -lA -lB vs -lB -lA. Changing header files around won't make any difference. Changing code won't help unless he never defined the "undefined/unresolved" elements.
Mr.Ree
drhorrible> Oups! Fixed.mrree> I'm not aware of that, but after reading your answer, I understand that it's a g++ specific problem (or a makefile system problem)? I'm more used to MSVC compilers and those exact same errors can occurs too, so maybe I misunderstood because they have the same desc?
Klaim
Perhaps. I'm not familar with the MSVC compiler errors. But when he mentioned "during linkage" and "during runtime after dlopen", I figured it was the usual misordering of libraries. See: http://stackoverflow.com/questions/492374/g-in-what-order-should-static-and-dynamic-libraries-be-linked
Mr.Ree
I see, then I should add that it's my answer IF it was in the case of MSVC. I'll delete this answer as soon as Micheal add the precisions in the question.
Klaim