问题
When we work on Turbo C, we get all the functions and header files by default which we can include normally by #inlcude eg: stdlib.h, math.h
But when writing a simple program using such header files I am getting error because I'm unable to include these files. Aren't these header files available by default for us to use? If yes then how to use such header files? When I used a function sqrt in "math.h" I was getting error as math.h was not getting included so I had to include it in the following command:
cc -c aaa.c -I/usr/local/ssl/include
gcc -o aaa aaa.c -I/usr/local/ssl/include -L/usr/local/ssl/lib -lcrypto -lm
./aaa
In this command the 2nd one is having -lm at the end to include math.h
again similarly I used a function itoa() which is in stdlib.h which I am executing on a UNIX Solaris server, but it is not getting included and I am gettig error. Now I don't know how to add this header file.
回答1:
The math.h
header is included normally. The code can compile. However, the compiler won't find the compiled binary (the implementation of math.h) to link to unless you specify it to do so. So you have to specify -lm in the command.
itoa()
is not a standard function in stdlib.h
, so do not use it. You can use sprintf instead.
回答2:
your compiler should provide command line settings where you can specify include directories, library directories etc. it is best if you take a look at your compiler documentation.
E.g. visual studio has a command switch -I to specify include folders
alt. in some cases it can be specified as an environment variable e.g. set INCLUDE=...
it all depends on what compiler you use.
回答3:
By default these are standard library functions that are exported from libc and they should be available in any Unix/Linux flavor. You can check where the header file exists using command like
find / -name "stdio.h" 2>/dev/null
Also make sure you link to libc using -l libc
Also, which compiler are you using? I suggest you use gcc, that way the include config is already done for the compiler, you can use it as is and get going.
来源:https://stackoverflow.com/questions/10715572/how-to-include-the-basic-header-files-which-are-available-with-c-in-unix