Monday, May 08, 2006

C++ compiler(gcc) basic how to's

gcc works as standard compiler for many languages, but here im just focusing on command line options used by c++ language, so
follows a simple command which compile a c++ source file called 'simple.cpp' which implements elements from the Standard Template Library(STL) ;in fact simple.cpp begins including header '#include' to make use of standard objects, as streaming output and so on.
_command:
gcc simple.cpp -lstdc++ -static -o simple
_explanation:
-l+stdc++ -> '-l' is the prefix for calling libraries followed by the name of the library u want to include(stdc++ in this case);
'-static' tells the compiler to link against the library in static mode(at load time), rather than in dynamic mode(at runtime)
'-o' specify the output file destination(the .exe extension is added by default)

That's all, just as quick reference after months without compiling anything!!!

UPDATE (compiling in WINDOWS environment with Win32 API)


assuming want to compile under windowsXP an indipendent executable linked against standard Microsoft DLL using GCC compiler released with CygWin;
gcc win.cpp -mno-cygwin -lwin32k -o win
_Explanation:
win.cpp -> c++ source file
-m -> hardware models & configuration -> no-cygwin -> dont link against Cygwin API
-l -> options for linking -> win32k -> DLL win32 API
-o -> output file (otherwise output to stream out)

UPDATE (avoiding include the '-lstdc++' option) & (compiling with custom header)

'gcc' command invokes just the frontend of the compiler, but if u need to compile just C++ source files, it's a best practice invoking 'g++' command insted; this way it's no more needed the '-lstdc++' option which is included by default.

When splits code in multiple files organized by headers, u need to pass the implementation file to the compiler just before the source file storing the 'main' entry point. Assuming sample.cpp the implementation file, sample_app.cpp the entry point file, the command to pass comes:
g++ sample.cpp sample_app.cpp -o sample

No comments:

Post a Comment