I've noticed that gcc requires one to specify flags for .h library files ... such a pain. :rolleyes: Can anyone tell me where I might find an exhaustive listing of all these flags? If it's somewhere in standard documentation, it must be very poorly marked, because I can't seem to find it!

EDIT: Yes, it's painfully obvious that I'm a noob to programming in C ... Anyone know of some good tutorial sites?

08-15-2010

dwks

What do you mean, gcc requires flags to be specified for .h files? Certainly you can give gcc flags to affect its behaviour, but flags aren't required. Are you perhaps trying to compile a header file? (Because you don't compile header files, you only compile .c files into .o files, and then link the .o files together to form an executable.)

If you have all your code in one directory, you should be able to compile it with something as simple as

Code:

gcc *.c -o executable

unless you're doing something complicated, which you shouldn't be if you're just learning C.

08-15-2010

kermit

I wonder if he is referring to things like having to use the -lm switch for math.h, and such.

08-15-2010

LinuxSmith

Quote:

Originally Posted by dwks

What do you mean, gcc requires flags to be specified for .h files? Certainly you can give gcc flags to affect its behaviour, but flags aren't required. Are you perhaps trying to compile a header file? (Because you don't compile header files, you only compile .c files into .o files, and then link the .o files together to form an executable.)

If you have all your code in one directory, you should be able to compile it with something as simple as

Code:

gcc *.c -o executable

unless you're doing something complicated, which you shouldn't be if you're just learning C.

It didn't work like that at with the <math.h> library functions ... I had to use a -lm parameter flag to make it one of my first programs compile at all.

...Is there perhaps a way to avoid this tediousness, or again, where can I find such a flag listing?

EDIT: That is *exactly* what I'm referring to kermit!

08-15-2010

rags_to_riches

-lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.

-lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.

So where might I find a listing of all common pre-defined library names then?

08-15-2010

rags_to_riches

How about the man pages? For example, man atan. Notice how it says "Link with -lm"?

Bottom line is, the libraries used will vary from project to project. For example, I never use the math libraries, but I often use sockets (libsocket) or openssl (libssl and libcrypto). The individual libraries should tell you to with what you need to link.

EDIT: Also in an attempt to further straighten you out on the compilation process: .h files are NOT library files; they are header files. Header files DEFINE the function (create a prototype) for the compiler, so the compiler knows if you're calling them correctly. The functions themselves are in the library file (.a file) which are brought into the program by the linker in the final step of the process.

08-15-2010

kermit

Quote:

Originally Posted by rags_to_riches

-lm is NOT a "parameter flag". It says search the library paths (specified with -L) for a library file with the name libm.a and link with it. If you wanted to link with a library called libfrobdignazzle.a, you would use -lfrobdignazzle.

LinuxSmith, have a look at the ld(1) man page, either on your own system, or here. Do a search for this (read the paragraph that follows it):

-l namespec
--library=namespec

Quote:

Originally Posted by LinuxSmith

So where might I find a listing of all common pre-defined library names then?

It is not so simple as that - for any library that is not standard, you will have to use the -l switch to tell the linker to look for something that is not standard. For example, when compiling OpenGL (with SDL) stuff under linux, you would see lots of -l switches:

Code:

-lglut -lXext -lX11 -lXmu -lXi -lGL -lGLU -lm -lSDL -lSDL_mixer

rags_to_riches is correct though - the man pages for the functions you are using ought to tell you when you need to use a switch like that.

As an aside, compile the following program, (using the -lm switch), and then run it with ldd.

As you can see, my program needed libm, which is why I needed to use -lm.

I am not sure why math.h is considered non-standard - it seems to me that this is a carry over from old Unix behaviour that was long since unnecessary. You can read a little more about some of those quirks (though no explanation as to why) in the FAQ (follow the links through as well).

Edit: There is a discussion on stackoverflow about why we still need to manually link libm. Lots of ideas to consider, if nothing else. Read it here.