//filename: hello.c //========================================================= main () { printf("Hello World!\n"); } //=========================================================
argc
and argv
, illustrated in the following:
//========================================================= main ( argc, argv) int argc; char **argv; { char *PROGNAME = *(argv++); if (argc > 2) { fprintf (stderr, "format: %s [input file]\n", PROGNAME ); exit ( -1 ); } if (argc == 2 && (input = fopen ( *argv, "r")) == NULL) { fprintf ( stderr, "%s: cannot open %s for reading\n", PROGNAME, *argv ); exit ( -1 ); } } //=========================================================
argc
is the number of command line arguments
when your program is invoked, and argv
is an
array containing these command line arguments.
NOTE that the name of your program (i.e., the executable
code) is regarded as the FIRST argument, i.e., it is
stored in argv[0]. So argc
is always positive.
//========================================================= main ( argc, argv, envp) int argc; char *argv[]; char **envp; // Note that **char and *char[] are equivalent {... } //=========================================================
#define PI 3.1415
const float pi = 3.1415;
#define macro_name(arg1, arg2, ...) string_definition
#define SQUARE(x) x * x while (count <= 5) { printf("%d\t%d\n", count, SQUARE(count)); count = count+1; }
#define SQUARE(x) ((x) * (x))
#includein your file.c, then you should call
% gcc file.c -lm ...NOTE: the order of placement of "-lm" is important! E.g., %gcc -lm file.c ... will not work here.
% gcc file.c -L/home/yap/lib -lm -lmyLib ...
LD_LIBRARY_PATH=/usr/local/lib:\ /usr/openwin/lib:\ /usr/motif/lib:\ /opt/SUNWspro/lib:\ /usr/unsupported/installers/chenli/libThe last one (../chenli/lib) is needed for openGL and glut libraries.
>ldd fooand this information will be displayed. E.g., on solaris:
> yap@jinai[173]% ldd rotate2 > libGL.so => /usr/unsupported/packages/opengl/Mesa/lib/libGL.so > libGLU.so => /usr/unsupported/packages/opengl/Mesa/lib/libGLU.so > libXmu.so.6.0 => /usr/local/lib/libXmu.so.6.0 > libXi.so.6.0 => /usr/local/lib/libXi.so.6.0 > libXext.so.6.4 => /usr/local/lib/libXext.so.6.4 > libX11.so.6.1 => /usr/local/lib/libX11.so.6.1 > libm.so.1 => /usr/lib/libm.so.1 > libsocket.so.1 => /usr/lib/libsocket.so.1 > libnsl.so.1 => /usr/lib/libnsl.so.1 > libc.so.1 => /usr/lib/libc.so.1 > libSM.so.6.0 => /usr/local/lib/libSM.so.6.0 > libICE.so.6.3 => /usr/local/lib/libICE.so.6.3 > libpthread.so.1 => /usr/lib/libpthread.so.1 > libXt.so.6.0 => /usr/local/lib/libXt.so.6.0 > libdl.so.1 => /usr/lib/libdl.so.1 > libmp.so.2 => /usr/lib/libmp.so.2 > /usr/platform/SUNW,Ultra-5_10/lib/libc_psr.so.1 > libthread.so.1 => /usr/lib/libthread.so.1 > yap@jinai[174]% >
> objdump -t /lib/libc.a | grep "fork"This will print all the library routines that has name containing "fork".
> ldd fooand this information will be displayed.
> gcc -v test.c
> gcc -### test.cwhere "###" is literally what you type! Each argument is written in a quoted string.
> gcc -print-file-name=libc.aOn my cygwin, it prints:
/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/../../../libc.awhich is really /usr/lib/libc.a.
> gcc -print-search-dirs 2>&1 | tee xThe following output is stored in file "x", which you can examine:
install: /usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/ programs: =/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/ :/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/ :/usr/lib/gcc-lib/i686-pc-cygwin/:... libraries: =/usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/: /usr/lib/gcc/i686-pc-cygwin/3.3.3/:...
> gcc -print-file-name=libc.a test.cand gcc will print the full absolute path name of the library that would be used for linking. It does nothing else! See man gcc for more options.
GCC_EXEC_PREFIX COMPILER_PATH LIBRARY_PATH LANG
> gcc -print-file-name=libc.a /usr/lib/gcc-lib/i686-pc-cygwin/3.3.3/../../../libc.a > gcc -B/usr -print-file-name=libc.a /lib/libc.a Note that the -B flag changed the search path for "libc.a".NOTE: in cygwin, the directories /usr/lib and /lib are identical. E.g., create a dummy file in /usr/lib, and it shows in /lib; delete in one and it is also deleted in the other!
#define INFILE "scores.txt" FILE *fhandle; fhandle = fopen(INFILE, "r"); if (fhandle == NULL) { perror(INFILE); exit(EXIT_FAILURE); }
//============================================ #includeRef [Jumpstart] gives the following code, which it says is equivalent to the library function "perror()":#include ... int code = system_call(); if (code == -1) { extern int errno; extern char * sys_errlist[]; printf("%s\n", sys_errlist[errno]); exit(1); } ... //============================================
//============================================ void report_err(char * prefix) { extern int errno; extern int sys_nerr; // size of array sys_errlist extern char *sys_errlist[]; if (prefix != NULL) printf("%s: ", prefix); if (0 < errno && errno < sys_nerrr) printf("%s\n ", sys_errlist[errno]); else printf("unknown error\n"); exit(1); } //============================================
GO TO TOP Please send comments to yap at cs dot nyu dot edu.