gcc and linking external libraries

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

msimmons
Posts: 11
Joined: Wed Dec 12, 2007 5:32 am UTC

gcc and linking external libraries

Postby msimmons » Mon Jan 14, 2008 1:56 am UTC

Basically, how do I know which one to link? For example if I have 3 simple files, all empty, a xxx.cc file, a main.cc file, and a xxx.hh file, and I stick in #include <iostream> in the header file, it gives the whole "undefined refrence to ZZZ" for a ton of options.

So how can I gather which library to use in the linking stage? Internet resources are good, if you know any.

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: gcc and linking external libraries

Postby phlip » Mon Jan 14, 2008 2:06 am UTC

Chances are you're using gcc rather than g++ - use the latter for compiling C++ code, it automatically links to the standard C++ libraries.

You can also use "gcc -lstdc++ whatever.cpp" and it'll do the same thing... but g++ is easier to remember (and it might link to other stuff as well as libstdc++, I don't know).

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Dakman
Posts: 50
Joined: Sat Jul 07, 2007 7:49 am UTC

Re: gcc and linking external libraries

Postby Dakman » Mon Jan 14, 2008 4:03 am UTC

You really probably don't have to link to libc. If you are asking how to compile multiple files do something like this

g++ -c xxx.cc -o xxx.o -I.
g++ -c main.cc -o main.o -I.
g++ -o application main.o xxx.o

msimmons
Posts: 11
Joined: Wed Dec 12, 2007 5:32 am UTC

Re: gcc and linking external libraries

Postby msimmons » Mon Jan 14, 2008 5:59 pm UTC

You're right, with g++ I don't need to link the libraries.
Which is why I've been using it to test my code ^.^

Unfortunately, the people who my professor I'm doing research for use gcc, so I'd like to be able to compile it with gcc.

So right now my makefile looks like: (I removed whitespace to conserve space on the board)

Code: Select all

#CCC = gcc
CCC = g++
FILES = main
all: ${FILES}
main: driver.o testwf.o functions.o
   ${CCC} driver.o testwf.o functions.o -o main
driver.o: driver.cc testwf.hh functions.hh
   ${CCC} -c driver.cc
testwf.o: testwf.cc testwf.hh functions.hh
   ${CCC} -c testwf.cc
functions.o: functions.cc functions.hh
   ${CCC} -c functions.cc
clean:
   rm -f *.o *~ *.exe


should I just add in the -lstdc++ where I build the object files?
for example something like

Code: Select all

CCC = gcc
#CCC = g++
...
FLAGS = -lstdc++
...
driver.o: driver.cc testwf.hh functions.hh
   ${CCC} ${FLAGS} -c driver.cc
(except for every one of them)

Edit: That's how I was taught to do it, so Dakman if there's a way using gcc you could point out, anything that works is fine with me =)

Rysto
Posts: 1460
Joined: Wed Mar 21, 2007 4:07 am UTC

Re: gcc and linking external libraries

Postby Rysto » Mon Jan 14, 2008 6:16 pm UTC

g++ is gcc. I fail to see the issue with using g++.

msimmons
Posts: 11
Joined: Wed Dec 12, 2007 5:32 am UTC

Re: gcc and linking external libraries

Postby msimmons » Mon Jan 14, 2008 6:22 pm UTC

Rysto wrote:g++ is gcc. I fail to see the issue with using g++.

Well that certainly makes things easier, I'm just going off what I was told, and I have no way of knowing it was the same thing

User avatar
segmentation fault
Posts: 1770
Joined: Wed Dec 05, 2007 4:10 pm UTC
Location: Nu Jersey
Contact:

Re: gcc and linking external libraries

Postby segmentation fault » Mon Jan 14, 2008 7:17 pm UTC

use g++ with iostream

i believe you can use gcc with iostream.h. EDIT: no, you cant :(

either way, whoever said g++ automatically links to c++ std libraries i think is correct. gcc wont do that, which is why you get undefined references.
people are like LDL cholesterol for the internet

User avatar
phlip
Restorer of Worlds
Posts: 7572
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: gcc and linking external libraries

Postby phlip » Mon Jan 14, 2008 8:57 pm UTC

msimmons wrote:should I just add in the -lstdc++ where I build the object files?
for example something like

Code: Select all

CCC = gcc
#CCC = g++
...
FLAGS = -lstdc++
...
driver.o: driver.cc testwf.hh functions.hh
   ${CCC} ${FLAGS} -c driver.cc
(except for every one of them)

No, just for the linker step:

Code: Select all

CC = gcc
LINKFLAGS = -lstdc++

main: somefile.o
   ${CC} ${LINKFLAGS} -o main somefile.o
somefile.o: somefile.cpp somefile.h
   ${CC} -c -o somefile.o somefile.cpp

But yes, g++ is gcc, it just automatically links to that library, and using it instead should be fine (if it isn't, complain to your professor).
From the man page: "g++ is a program that calls GCC with the default language set to C++, and automatically specifies linking against the C++ library."
The default language thing doesn't matter since you're indicating it's C++ with the filename extension (gcc will recognise .cc, .cp, .cpp, .cpp, .c++ or .C files as C++)... so the only thing it changes is the library.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: gcc and linking external libraries

Postby EvanED » Mon Jan 14, 2008 9:41 pm UTC

I'll also point out that you are breaking a number of makefile conventions.

First:
- Flags passed to the compiler are traditionally called CFLAGS or CCFLAGS (for C and C++ code respectively)
- Flags passed to the linker are traditionally called LDFLAGS

Second:
- Make already has a number of built in generic rules. For instance, it knows that if you need a file called blah.o and there is a file called blah.cc, it can compile it using the rule $(CXX) $(CXXFLAGS) $(CPPFLAGS) -o -c blah.o blah.cc. Then you just have to tell it additional dependencies. It knows how to produce a file called just blah from a file blah.o. (Unfortunately, with "$(CC) $(LDFLAGS) blah.o" which won't work directly here. It also won't work if none of the .o files have the same name as the target.)

So I can simplify that makefile to the following:

Code: Select all

main: driver.o testwf.o functions.o
   $(CXX) $(LDFLAGS) $^ -o $@ $(LOADLIBES) $(LDLIBS)

driver.o: testwf.hh functions.hh
testwf.o: testwf.hh functions.hh
functions.o: functions.hh

.PHONY: clean
clean:
   rm -f *.o *~ *.exe


The line that builds the exe ($(CXX) $(LDFLAGS) $^ -o $@ $(LOADLIBES) $(LDLIBS)):
  • Uses the C++ compiler ($(CXX)) instead of the C compiler ($(CC))
  • Passes the contents of LDFLAGS (empty for now, but you can set it later, or it can be passed on the command line when you run make)
  • $^ is a variable that is replaced by the current dependencies. In this case, it will expand to "driver.o testwf.o functions.o". By using this instead of listing all of them again, you (1) avoid repetition, (2) make copy-and-pasting to other projects easier, and (3) make it so if you add a new .o file, you don't need to change this line.
  • $@ is a variable that is replaced by the target. In this case, it expands to "main". Using it has the same benefits as above.
  • LOADLIBES and LDLIBS are there to keep parity with the automatic rule in GCC that builds blah from blah.o

For each of driver.o, testwf.o, and function.o, we only need to list dependencies other than the corresponding .cc file, and we use the built-in rules.

Finally, the .PHONY line says that the clean rule doesn't actually generate a file called "clean", and should always be run. Without this, if you create a file called "clean", "make clean" will no longer run because it will think that clean is up to date.

If you wanted to build it running 'gcc' instead of 'g++', you could add the following two lines at the top of the makefile:

Code: Select all

CXX=gcc
LDFLAGS=-lstdc++


You can't always simplify like this, but in simple cases you can. For more complex ones, you can define your own pattern rules. (E.g. "make any target ending in .yoda from a source with the same stem ending in .jedi using this rule".) This is when automatic variables like $^ and $@ become absolutely essential.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: gcc and linking external libraries

Postby Pesto » Mon Aug 31, 2009 6:25 pm UTC

I have a related question not worth its own thread.

I've been doing some stuff in C. Some of my programs use stdio.h and math.h. Both headers are in the standard C library, so why is it that I have to explicitly link to the math library with the "-lm" directive, but stdio.h is linked automatically?

I get what's going on, I just don't get why gcc is designed to behave like this.

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: gcc and linking external libraries

Postby stephentyrone » Mon Aug 31, 2009 8:12 pm UTC

Pesto wrote:I have a related question not worth its own thread.

I've been doing some stuff in C. Some of my programs use stdio.h and math.h. Both headers are in the standard C library, so why is it that I have to explicitly link to the math library with the "-lm" directive, but stdio.h is linked automatically?

I get what's going on, I just don't get why gcc is designed to behave like this.


Long story. Short answer: so that it's marginally easier to use an alternative libm if you want to.
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: gcc and linking external libraries

Postby Pesto » Mon Aug 31, 2009 10:13 pm UTC

How do I determine which are the automagically linked libraries (like stdio.h) and which aren't (like math.h)?

I haven't found anything in the gcc manual, and I don't know where else I might look.

stephentyrone
Posts: 778
Joined: Mon Aug 11, 2008 10:58 pm UTC
Location: Palo Alto, CA

Re: gcc and linking external libraries

Postby stephentyrone » Mon Aug 31, 2009 10:19 pm UTC

Pesto wrote:How do I determine which are the automagically linked libraries (like stdio.h) and which aren't (like math.h)?

IIRC the correct answer to this is "every standard library that isn't the math library"
GENERATION -16 + 31i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

User avatar
PM 2Ring
Posts: 3713
Joined: Mon Jan 26, 2009 3:19 pm UTC
Location: Sydney, Australia

Re: gcc and linking external libraries

Postby PM 2Ring » Tue Sep 01, 2009 3:37 pm UTC

I get what's going on, I just don't get why gcc is designed to behave like this.
It's traditional. Some early C compiler for microcomputers didn't even have floating point support (or handled it using various horrible techniques). And I suspect that these days there are plenty of embedded processors that don't have floating-point hardware or software. So C compilers have traditionally required you to be explicit about which maths library you want to link to, if any. FWIW, I've been programming in C since 1980, and I still have the habit of avoiding the use of floating-point when practical.

User avatar
Pesto
Posts: 737
Joined: Wed Sep 05, 2007 5:33 pm UTC
Location: Berkeley, CA

Re: gcc and linking external libraries

Postby Pesto » Tue Sep 01, 2009 6:12 pm UTC

That makes a lot of sense. Thanks.

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: gcc and linking external libraries

Postby Karrion » Thu Sep 03, 2009 6:03 am UTC

Pesto wrote:How do I determine which are the automagically linked libraries (like stdio.h) and which aren't (like math.h)?


Be careful not to confuse two different concepts here. stdio.h and math.h are header files which provide declarations of functions (etc). Libraries (which you specify at link time) provide the binary implementations of functions. They're related, but it's not a one-to-one correspondence. A library might provide functions whose declarations are in several different header files, or a header file might declare functions that require multiple libraries to be linked.

As to how you know what headers you need to #include, and which libraries to link, it should be in the documentation (man page, MSDN documentation or whatever) for the functions you need.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 4 guests