5

I am having a hard time figuring out what flags to pass to g++ when performing linking. Basically, I compile some code with these "standard" flags:

CXXFLAGS = -Wall -Wextra -Wconversion -pedantic -std=c++0x -O2

and, afterwards, I merge the produced .o files into several static libraries like so:

libxxx.a: xxx1.o xxx2.o ...
    ar rcs $@ $^
libyyy.a: yyy1.o yyy2.o ...
    ar rcs $@ $^
...

Questions:

  • Do I need to use the -static flag in CXXFLAGS when compiling the .o files?

After the static libraries are created, I want to link some compiled .o files together with some of these libraries in order to build an executable so I use this:

LINKER = g++
LIB_DIR = lib/linux
SYSTEM_LIBS = -lgmp
LDFLAGS = -Wall -L $(OUTPUT_DIR) -L $(LIB_DIR) $(SYSTEM_LIBS)
$(LINKER) $^ $(LDFLAGS) -lsvm -lUtils -lKinderedSpirits -o $@

exe:
    $(LINKER) o1.o o2.o $(LDFLAGS) -lxxx -lyyy -lzzz -o $@

Questions:

- Should I use the -static flag here? - Does -Wall make any sense here or is it useful just for compiling? - Are there any other "standard" flags that need to be passed to the linker, similar to the ones recommended for the compiler?

Also, during linking it's giving me exceptions about undefined references from the GMP library. As far as I can tell, -lgmp is sent to the linker and it is installed on the system (I was able to compile a simple hello world which uses GMP from the command line) and libxxx.a libyyy.a libzzz.a are located in $(LIB_DIR). Maybe I should mention that the GMP symbols are used in libxxx.a.


UPDATE:

I managed to fix the undefined references for the GMP symbols. The issue was caused by the order in which I placed the libraries. Basically, as specified here, I need to reference the libraries that depend on GMP before -lgmp. Anyway, I'm still looking for answers to my 3 questions above.

Community
  • 1
  • 1
Mihai Todor
  • 8,014
  • 9
  • 49
  • 86

1 Answers1

2

Q: Should I also use the -static flag here? A: Probably not necessary. This flag just makes it impossible to accidentally link in dynamic libraries.

Q: Does -Wall make any sense here or is it useful just for compiling? A: I believe it's just for compiling. (fyi, the capital W followed by the word "all" species you want all warnings during compilation)

Q: Are there any other "standard" flags that need to be passed to the linker, similar to the ones recommended for the compiler? A: Not that I'm aware of. You can find common options here: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

Are you running into any specific error you'd care to paste?

loki11
  • 374
  • 1
  • 4
  • Now it's compiling & linking just fine, but I wanted to make sure that I'm doing it right, though. Regarding the link, I can use Google myself, thanks, but I was wondering if there are some layman's guides on what linker flags are considered "standard" and must (almost) always be used, like the compiler flags that I mentioned. – Mihai Todor Jul 01 '12 at 00:32
  • 1
    Generally speaking, command line arguments are there for you to tell the compiler what you want. If you don't yourself know what you want, then that's that. Either you have a reason for a certain flag, or you don't. If you do, you know. If you don't, why make one up? Perhaps you should scour the gcc manpage to figure what's there to begin with. – Kuba hasn't forgotten Monica Aug 08 '12 at 13:28
  • The concept of "standard" flags that must "(almost) always" be used is something that, seriously, doesn't exist except in the comment above. You can compile and link perfectly good projects using nothing but the flags that tell gcc what the input and output files are. If you have a particular problem, for example you want to be informed about some possible problems in your code, then the particular solution is the -Wall flag. The correct way of proceeding is thus: 1. Understand your requirements. 2. Apply flags as requirements dictate. – Kuba hasn't forgotten Monica Aug 08 '12 at 13:31
  • 2
    Anything else would be a [cargo cult approach](http://www.lhup.edu/~dsimanek/cargocul.htm). That's where you copy what others are doing without understanding what's going on. That's a mostly unproductive approach, but sadly popular. – Kuba hasn't forgotten Monica Aug 08 '12 at 13:32
  • @KubaOber +1 for the "cargo cult approach". You should have added this in an answer :) But, you see, this is the thing: the man pages do not help me to figure out if, for example, -Wall has any purpose whatsoever during linking, so I was hoping that someone could, at least, point me to a list where I can find common (compiler and linker) GCC flags, compiler-specific GCC flags and linker-specific GCC flags. I don't want to dig through GCC's code to figure such things out... – Mihai Todor Aug 09 '12 at 22:02
  • 2
    Admittedly, gcc's manpage doesn't explicitly say that -Wall does *not* affect the linker. Yet, if you look at the options, the linker options are very limited and no warnings are listed there at all that's the first hint. If you search for -Wall in the manpage, you'll see that it only enables preprocessor and C/C++ compiler flags. By elimination, it doesn't enable warnings anywhere else. It's easy enough to put a short script in your path before anything else to see what flags gcc calls the linker with to convince yourself that -Wall applies only to the preprocessing/compilation. – Kuba hasn't forgotten Monica Aug 10 '12 at 03:01