tags:

views:

124

answers:

3

in these i'm playing with thread library and trying to implement some functions. One of the tutorial says that to run the program use :

gcc -lpthread -lrt -lc -lm project1.c scheduler.c -o out

first of all i need deep understanding of what is gcc doing in each line,

  • lpthread is used for what? what are the contributions of lrt -lc -lm ?

  • project1.c and scheduler.c is compiled together so what should i understand? i checked
    the code and any of them not included in project1.c or scheduler.c.

  • as an output clearly it gives "out".

secondly the author states that to run the program you have to use

./out number filename (For example, ./out 2 sample.txt)

To make these clear as far as i understand the main function gets number and sample.txt as an input.(?)

thanks for your answers and making me clear.

+4  A: 

-l parameter means - link to a specific library. See GCC manual for more information

Thus -lpthread means link to libpthread.so (or pthread.a) Likewise for -lm -lrt, -lc

[lib]pthread[.so] - POSIX threads

[lib]m[.so] - math standard library (sin, cos, e.t.c.)

[lib]rt[.so] - POSIX realtime extensions

[lib]c[.so] - libc (standard C library)

Each of your .c files are compiled to .o object file (these are called compilation units) and are linked together with the above mentioned libraries.

You are right about how command line parameters should be passed.

EFraim
@EFraim, thanks, the unclear issue is why linking? if i #include library in my .c file, why i need to use it? can you suggest me some tutorials the way c,c++ handles these issues? (i think i have some problems to understand the details of how c,c++ compiles)
berkay
@Berkay: The headers usually only *declare* the procedures and global variables. (The function signatures are without bodies). By linking with a library you supply the actual body to *resolve* the otherwise unresolved reference.That is, by including the header you promise the compiler you do have such a function. The compiler leaves a reference, later filled by the linker from the library.
EFraim
This chapter, http://publications.gbdirect.co.uk/c_book/chapter4/linkage.html , about linking can help.
EFraim
+3  A: 

-lxxx means to link a library named libxxx, so you're telling it to link libpthread, librt, libc, and libm.

It seems unlikely that you really need to specify linking libc -- that normally happens by default. libm is the math library, so you need it for most code that does things like floating point. libpthread includes the pthreads functions (e.g., pthread_create) so any code that creates/uses pthreads needs to link to it.

project1.c and scheduler.c being compiled together means that code in one can call functions that are in the other.

Your understanding of command line parameters seems correct.

Jerry Coffin
@Jerry Coffin, project1.c and scheduler.c being compiled together means that code in one can call functions that are in the other :so i understand that some extern functions are in use.
berkay
@Berkay: yes, that's correct.
Jerry Coffin
+1  A: 

as an output clearly it gives "out"

Yes you specified this with -o out


gcc does not only compile your .c files to .o files but also links the compiled files and the libraries you specified with -l together to a binary file.


./out number filename (For example, ./out 2 sample.txt)

Number and filename are two parameters for your program with the name out.

TheMorph
why should I be ANY kind of an bot? ;)
TheMorph
@TheMorph, thanks for your answer, i appreciate you can only make the unclear issues instead replicating.
berkay