开发者

Demystifying gcc under lpthreads

开发者 https://www.devze.com 2022-12-30 14:16 出处:网络
in these days i\'m playing with thread library and trying to implement some functions. One of the tutorial says that to run the program use :

in these days 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 unders开发者_JAVA技巧tanding 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.


-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.


-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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号