User:Phosgram

From Wikibooks, open books for an open world
Jump to navigation Jump to search

...

GCC Debugging

GCC Notes

Compiler Commands[edit | edit source]

Commands run from a terminal or command line/prompt.

gcc Commands[edit | edit source]

gcc (GNU C Compiler)

Basic Compile to object code (.o file):

gcc -c filename.c

Basic Compile to executable:

gcc -o "progname" "folder\filename.c"

Error checking:

gcc -Werror -Wall -o progname "folder\filename.c"

Full, Error-Checking Compile:

gcc -Werror -Wall -pedantic -o "folder\filename.c"

C99 Compliant Compile:

gcc -std=gnu99 -o "progname" "folder\filename.c"

Print the output of the preprocessor:

gcc -E "folder\filename.c"

GDB Debugger ready compile:

gcc -o "progname" -g "folder\filename.c"
gcc -g "folder\filename.c"

Multi-file build with GCC:

gcc -o progname source1.c source2.c source3.c source4.c

g++ Commands[edit | edit source]

Generating a '.o' file

g++ -c myprog.cpp

Generating an executable

g++ <arguments> <source file name> <arguments> <output file name>
eg: g++ -Wall myprog.cpp -o myprog
eg: g++ -Wall -Werror myprog.cpp -o myprog
eg: g++ -pedantic-errors myprog.cpp -o myprog

Multi-file build

g++ -o EXECUTABLE_NAME 1ST_FILENAME 2ND_FILENAME NTH_FILENAME
eg: g++ -o myprogram myprog.cpp testmyprog.cpp

Running compiled programs in Unix

$ ./<PROGRAM_NAME>
eg: $ ./myprog

Makefiles[edit | edit source]

Creating a makefile

  • Note, must be named "makefile" (no extension)
  • Note, must be in sourcefile directory

Basics[edit | edit source]

Basic example:

test1:
	echo Hi There!

C++ example:

myprog:
	g++ -o myprog myprog.cpp testmyprog.cpp

C / C++[edit | edit source]

Sample structure:

FILE_EXTENSION_TO_LOOK_FOR = g++

1ST_O_FILE_NAME_TO_BUILD.o: <TAB> FILENAME HEADER_FILENAME(S)

2ND_O_FILE_NAME_TO_BUILD.o: <TAB> FILENAME HEADER_FILENAME(S)

EXECUTABLE_NAME: <TAB> 2ND_O_FILE_NAME.o 1ST_O_FILE_NAME.o

Example:

CXX = g++
CC = g++

myprog.o:	myprog.cpp myprog.h

testmyprog.o:	testmyprog.cpp myprog.h

myprog:		myprog.o testmyprog.o

Compiling a makefile

Make

make PROGRAM_NAME_IN_MAKEFILE
eg: make myprog

MinGW (32 bit version):

mingw32-make PROGRAM_NAME_IN_MAKEFILE
eg: mingw32-make myprog

I used the sample makefile setup, but it is not generating an executable, how do I get it to do that?

  • For the above makefile setup to create an executable, the executable must share the same name as one of the ".o" files.
  • eg: "myprog: myprog.o testmyprog.o" works, but "myprog1: myprog.o testmyprog.o" would not.