Results 1 to 9 of 9
As we know, if we use #include <math.h> we need to add -lm in gcc right? But some computers of lab needn't to add -lm. I have check the alias, ...
- 10-28-2008 #1
why some computers need -lm while others don't
As we know, if we use #include <math.h> we need to add -lm in gcc right? But some computers of lab needn't to add -lm. I have check the alias, but there is no alias in there? Why it could be like this?
- 10-28-2008 #2
Well according to the C standard the math library should be include in the standard C library so you shouldn't have to link it separately because the stdlib is link automatically. This being said, every distro of Linux I programmed on required the math library to be linked separately....Hope this helps
- 10-28-2008 #3I don't think the C standard says this. In ANSI_ISO_IEC_9899-1999.pdf, Section 1 (entitled "Scope") says this:according to the C standard the math library should be include in the standard C library so you shouldn't have to link it separately because the stdlib is link automatically
On the other hand, the C standard is notoriously difficult to read, and maybe I'm missing something.This International Standard does not specify
- the mechanism by which C programs are transformed for use by a data-processing system ...
What am I missing? :)--
Bill
Old age and treachery will overcome youth and skill.
- 10-28-2008 #4
here's a quote from The C Programming Language by Brian W. Kerninghan & Dennis M. Ritchie
"The standard library is not part of the C language proper, but an environment that supports standard C will provide the function declarations and type and macro definitions of this library "
I hope that clarifies everything????
- 10-28-2008 #5
I don't get it. Firstly what does the -lm or -lX or -l(others) mean? I have read the man gcc, and I can't found any explanation about that. If I read the post above, it seems like -lm is something like linking object?
Second, the Univ is using one distro only (SUSe 10.0) why there is one needs -lm while other doesn't. I thought there is something with configuration??
- 10-28-2008 #6I found that at the beginning of Appendix B of K&R, second edition, and it doesn't seem to appear anywhere in the first edition, so I'm assuming you found it at the same place."The standard library is not part of the C language proper, but an environment that supports standard C will provide the function declarations and type and macro definitions of this library "
That statement is about function declarations and type and macro definitions, which are not directly related to the stage where the program is actually linked against the library.
Further, there is nothing that requires the standard library to be in a single library file.
That is indeed puzzling.the Univ is using one distro only (SUSe 10.0) why there is one needs -lm while other doesn't.
Curiouser and curiouser. But it could be a shell script instead, one which supplies the -lm for you.I have check the alias, but there is no alias in there
On the system which supplies the -lm for you, if you're using the gcc command, try this at the command line:
If you're using some other command, substitute that one in the above command line.Code:which gcc
Let's assume you get as output /usr/bin/gcc. In that case, do this at the command line:
If it's a symbolic link, then follow it. For example, if you get this output:Code:file /usr/bin/gcc
then enter this command:Code:/usr/bin/gcc: symbolic link to `gcc-4.2.3'
If you end up with something like this:Code:file /usr/bin/gcc-4.2.3
then abandon this line of inquiry. But if you get one of these:Code:/usr/bin/gcc-4.2.3: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), stripped
then use a text editor to examine this script. It may be supplying the -lm.Code:/usr/bin/gcc-4.2.3: ASCII text /usr/bin/gcc-4.2.3: Bourne shell script text executable
--
Bill
Old age and treachery will overcome youth and skill.
- 10-30-2008 #7
Hm.. I have checked the gcc and its result is
What do you mean by abandon the inquiry? I guess it doesn't link any file. So this mean I using the gcc directly isn't?/usr/bin/gcc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.2.5, dynamically linked (uses shared libs), stripped
Btw I checked the gcc by typing
gcc -v
and it show something like this :
Could it because the argument in configuration??Using built-in specs.
Target: i586-suse-linux
Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --libexecdir=/usr/lib --enable-languages=c,c++,objc,f95,java,ada --disable-checking --with-gxx-include-dir=/usr/include/c++/4.0.2 --enable-java-awt=gtk --disable-libjava-multilib --with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit --without-system-libunwind --host=i586-suse-linux
Thread model: posix
gcc version 4.0.2 20050901 (prerelease) (SUSE Linux)
- 10-30-2008 #8I mean, the system isn't set up to supply the -lm automatically by means of a shell script, so it doesn't pay to pursue that particular question.What do you mean by abandon the inquiry?
Correct. That means that your first file command is going to give you as much information as you're going to get from any file command.I guess it doesn't link any file.
Correct.So this mean I using the gcc directly isn't?
Good move. I would not have thought of that. It turns out that this doesn't give any useful information, but it helped me remember that the actual program which decides what library files to link is not gcc, but ld; gcc uses ld to perform the linking part of the preparation of the executable file.Btw I checked the gcc by typing
gcc -v
I did a
(on a stock Slackware 12.1) to see whether it would enlighten me. The man page didn't exactly answer your original question. But it told me that ld uses a "script command language", and that there is a "default linker script". I thought that this script might be where the -lm is included for you by your system administrator.Code:man ld
The man page gave me no clue as to where the default linker script might be. And it's quite likely that it will be in a different place for you than for me. Actually, I think there is no default linker script on my system. But I will tell you how I looked for it, and you can follow the same steps to look for it on your system.
My secret weapon of choice was a program called strace. If you wish to pursue the same line of inquiry that I did, I would invite you to stop reading my words at this point and do a
If you're not familiar with sed, also glance briefly at the output from this:Code:man strace
Then come back here. :)Code:man sed
...
Done reading? Good! Moving right along ...
I created this ultra-simple source file and named the file 1.c:
I compiled it with the following command:Code:int main(void) { return 0; } /* main() */
That gave me a list of approximately 100 files which were opened while the system was compiling the program.Code:strace -f gcc 1.c -o 1 2>&1 | grep open | sed -e 's/^.*"\(.*\)\".*$/\1/' | less
Please replicate this adventure and look over the list of files which are opened on the system which provides the -lm for you.
I found no file which seemed to be a default linker script file. Maybe you'll find one which provides the answer you seek.
I did find this, though:
If you don't find a linker script file, look for a file named "specs". Examine it. I did, and found (among other things) a collection of specifications of linker behavior. Here is the whole specs file for my system, with linker-related material highlighted in red:Code:/usr/lib/gcc/i486-slackware-linux/4.2.3/specs
There's a lot of stuff there, but I'd pay particular attention to the link-command item at the end.Code:*asm: %{v:-V} %{Qy:} %{!Qn:-Qy} %{n} %{T} %{Ym,*} %{Yd,*} %{Wa,*:%*} *asm_debug: %{gstabs*:--gstabs}%{!gstabs*:%{g*:--gdwarf2}} *asm_final: *asm_options: %a %Y %{c:%W{o*}%{!o*:-o %w%b%O}}%{!c:-o %d%w%u%O} *invoke_as: %{!S:-o %|.s | as %(asm_options) %|.s %A } *cpp: %{posix:-D_POSIX_SOURCE} %{pthread:-D_REENTRANT} *cpp_options: %(cpp_unique_options) %1 %{m*} %{std*&ansi&trigraphs} %{W*&pedantic*} %{w} %{f*} %{g*:%{!g0:%{!fno-working-directory:-fworking-directory}}} %{O*} %{undef} %{save-temps:-fpch-preprocess} *cpp_debug_options: %{d*} *cpp_unique_options: %{C|CC:%{!E:%eGCC does not support -C or -CC without -E}} %{!Q:-quiet} %{nostdinc*} %{C} %{CC} %{v} %{I*&F*} %{P} %I %{MD:-MD %{!o:%b.d}%{o*:%.d%*}} %{MMD:-MMD %{!o:%b.d}%{o*:%.d%*}} %{M} %{MM} %{MF*} %{MG} %{MP} %{MQ*} %{MT*} %{!E:%{!M:%{!MM:%{MD|MMD:%{o*:-MQ %*}}}}} %{remap} %{g3:-dD} %{H} %C %{D*&U*&A*} %{i*} %Z %i %{fmudflap:-D_MUDFLAP -include mf-runtime.h} %{fmudflapth:-D_MUDFLAP -D_MUDFLAPTH -include mf-runtime.h} %{E|M|MM:%W{o*}} *trad_capable_cpp: cc1 -E %{traditional|ftraditional|traditional-cpp:-traditional-cpp} *cc1: %(cc1_cpu) %{profile:-p} *cc1_options: %{pg:%{fomit-frame-pointer:%e-pg and -fomit-frame-pointer are incompatible}} %1 %{!Q:-quiet} -dumpbase %B %{d*} %{m*} %{a*} %{c|S:%{o*:-auxbase-strip %*}%{!o*:-auxbase %b}}%{!c:%{!S:-auxbase %b}} %{g*} %{O*} %{W*&pedantic*} %{w} %{std*&ansi&trigraphs} %{v:-version} %{pg:-p} %{p} %{f*} %{undef} %{Qn:-fno-ident} %{--help:--help} %{--target-help:--target-help} %{!fsyntax-only:%{S:%W{o*}%{!o*:-o %b.s}}} %{fsyntax-only:-o %j} %{-param*} %{fmudflap|fmudflapth:-fno-builtin -fno-merge-constants} %{coverage:-fprofile-arcs -ftest-coverage} *cc1plus: *link_gcc_c_sequence: %{static:--start-group} %G %L %{static:--end-group}%{!static:%G} *link_ssp: %{fstack-protector:} *endfile: %{ffast-math|funsafe-math-optimizations:crtfastmath.o%s} %{shared|pie:crtendS.o%s;:crtend.o%s} crtn.o%s *link: %{!static:--eh-frame-hdr} -m %(link_emulation) %{shared:-shared} %{!shared: %{!ibcs: %{!static: %{rdynamic:-export-dynamic} %{!dynamic-linker:-dynamic-linker %(dynamic_linker)}} %{static:-static}}} *lib: %{pthread:-lpthread} %{shared:-lc} %{!shared:%{mieee-fp:-lieee} %{profile:-lc_p}%{!profile:-lc}} *mfwrap: %{static: %{fmudflap|fmudflapth: --wrap=malloc --wrap=free --wrap=calloc --wrap=realloc --wrap=mmap --wrap=munmap --wrap=alloca} %{fmudflapth: --wrap=pthread_create}} %{fmudflap|fmudflapth: --wrap=main} *mflib: %{fmudflap|fmudflapth: -export-dynamic} *link_gomp: *libgcc: %{static|static-libgcc:-lgcc -lgcc_eh}%{!static:%{!static-libgcc:%{!shared-libgcc:-lgcc --as-needed -lgcc_s --no-as-needed}%{shared-libgcc:-lgcc_s%{!shared: -lgcc}}}} *startfile: %{!shared: %{pg|p|profile:gcrt1.o%s;pie:Scrt1.o%s;:crt1.o%s}} crti.o%s %{static:crtbeginT.o%s;shared|pie:crtbeginS.o%s;:crtbegin.o%s} *switches_need_spaces: *cross_compile: 0 *version: 4.2.3 *multilib: . ; *multilib_defaults: *multilib_extra: *multilib_matches: *multilib_exclusions: *multilib_options: *linker: collect2 *link_libgcc: %D *md_exec_prefix: *md_startfile_prefix: *md_startfile_prefix_1: *startfile_prefix_spec: *sysroot_spec: --sysroot=%R *sysroot_suffix_spec: *sysroot_hdrs_suffix_spec: *cc1_cpu: %{!mtune*: %{m386:mtune=i386 %n`-m386' is deprecated. Use `-march=i386' or `-mtune=i386' instead. } %{m486:-mtune=i486 %n`-m486' is deprecated. Use `-march=i486' or `-mtune=i486' instead. } %{mpentium:-mtune=pentium %n`-mpentium' is deprecated. Use `-march=pentium' or `-mtune=pentium' instead. } %{mpentiumpro:-mtune=pentiumpro %n`-mpentiumpro' is deprecated. Use `-march=pentiumpro' or `-mtune=pentiumpro' instead. } %{mcpu=*:-mtune=%* %n`-mcpu=' is deprecated. Use `-mtune=' or '-march=' instead. }} %<mcpu=* %{mintel-syntax:-masm=intel %n`-mintel-syntax' is deprecated. Use `-masm=intel' instead. } %{mno-intel-syntax:-masm=att %n`-mno-intel-syntax' is deprecated. Use `-masm=att' instead. }%{march=native:%<march=native %:local_cpu_detect(arch) %{!mtune=*:%<mtune=native %:local_cpu_detect(tune)}} %{mtune=native:%<mtune=native %:local_cpu_detect(tune)} *link_emulation: elf_i386 *dynamic_linker: %{muclibc:%{mglibc:%e-mglibc and -muclibc used together}/lib/ld-uClibc.so.0;:/lib/ld-linux.so.2} *link_command: %{!fsyntax-only:%{!c:%{!M:%{!MM:%{!E:%{!S: %(linker) %l %{pie:-pie} %X %{o*} %{A} %{d} %{e*} %{m} %{N} %{n} %{r} %{s} %{t} %{u*} %{x} %{z} %{Z} %{!A:%{!nostdlib:%{!nostartfiles:%S}}} %{static:} %{L*} %(mfwrap) %(link_libgcc) %o %{fopenmp:%:include(libgomp.spec)%(link_gomp)} %(mflib) %{fprofile-arcs|fprofile-generate|coverage:-lgcov} %{!nostdlib:%{!nodefaultlibs:%(link_ssp) %(link_gcc_c_sequence)}} %{!A:%{!nostdlib:%{!nostartfiles:%E}}} %{T*} }}}}}}
Hope this gives you a little more to consider.--
Bill
Old age and treachery will overcome youth and skill.
- 11-01-2008 #9
Whaoh... I have tried the way, but found no any clue too... I tried to find libm.so or math.h
Beside, there is no file called spec at here.I try to find it at
/usr/lib/gcc/i586-suse-linux/4.0.2
Btw, I also tried it at my computer(Kubuntu), and somehow I found my gcc also don't need to append gcc with -lm.
Btw, I found something interesting topic at here (although doesn't explain the anomaly of gcc without -lm) :
http://www.linuxforums.org/forum/lin...e-problem.html


Reply With Quote