I'm reading the book named "Advanced C and C++ compiling", by Milan Stevanovic
The following is the snapshot from the book, followed by the problem I'm facing.
Concept illustration: Demo Project
The development environment used to build this simple project will be based on the gcc compiler running onLinux. Listings 2-1 through 2-3 contain the code used in the demo project.
Listing 2-1. function.h
#pragma once#define FIRST_OPTION#ifdef FIRST_OPTION#define MULTIPLIER (3.0)#else#define MULTIPLIER (2.0)#endiffloat add_and_multiply(float x, float y);
Listing 2-2. function.c
int nCompletionStatus = 0;float add(float x, float y){ float z = x + y; return z;}float add_and_multiply(float x, float y){ float z = add(x,y); z *= MULTIPLIER; return z;}
Listing 2-3. main.c
#include "function.h"extern int nCompletionStatus = 0;int main(int argc, char* argv[]){ float x = 1.0; float y = 5.0; float z; z = add_and_multiply(x,y); nCompletionStatus = 1; return 0;}
Demo Project Preprocessing Example:
The gcc compiler provides the mode in which only the preprocessing stage is performed on the input source files:
gcc -i <input file> -o <output preprocessed file>.i
Unless specified otherwise, the output of the preprocessor is the file that has the same name as the input file andwhose file extension is .i
. The result of running the preprocessor on the file function.c
looks like that in Listing 2-4.
Listing 2-4. function.i
# 1 "function.c"# 1 "# 1 "# 1 "function.h" 1# 11 "function.h"float add_and_multiply(float x, float y);# 2 "function.c" 2int nCompletionStatus = 0;float add(float x, float y){ float z = x + y; return z;}float add_and_multiply(float x, float y){ float z = add(x,y); z *= MULTIPLIER; return z;}
More compact and more meaningful preprocessor output may be obtained if few extra flags are passed to the gcc, like
gcc -E -P -i <input file> -o <output preprocessed file>.i
which results in the preprocessed file seen in Listing 2-5.
Listing 2-5. function.i
(Trimmed Down Version)
float add_and_multiply(float x, float y);int nCompletionStatus = 0;float add(float x, float y){ float z = x + y; return z;}float add_and_multiply(float x, float y){ float z = add(x,y); z *= 3.0; return z;}
Obviously, the preprocessor replaced the symbol MULTIPLIER
, whose actual value, based on the fact that theUSE_FIRST_OPTION
variable was defined, ended up being 3.0
.
Problem:
When I compile the program as is using gcc, following is the error I am facingSnapshot from my terminal.
gcc -i function.c -o function.icc1: error: unrecognized command line option '-i'gcc function.c -o function.i/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:In function '_start':(.text+0x18): undefined reference to 'main'collect2: ld returned 1 exit status$pwd/home/adminuser/advance_compiling$lltotal 20drwxrwxr-x 2 adminuser adminuser 4096 Jan 10 23:51 ./drwxr-xr-x 26 adminuser adminuser 4096 Jan 10 23:57 ../-rw-rw-r-- 1 adminuser adminuser 216 Nov 15 08:58 function.c-rw-rw-r-- 1 adminuser adminuser 163 Jan 10 23:33 function.h-rw-rw-r-- 1 adminuser adminuser 257 Dec 28 06:46 main.c
How do I get rid of this and proceed in learning the course?Please suggest.