Skip to content

Commit 77939ad

Browse files
committed
Improve documentation.
1 parent 4226ca1 commit 77939ad

30 files changed

+176
-296
lines changed

.gitignore

-29
This file was deleted.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boilerplate/.gitignore

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "boilerplate"]
2+
path = boilerplate
3+
url = https://github.com/cirosantilli/cpp-boilerplate

Makefile_many

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
1-
# Compiles C files with GCC, and c++ files with g++, one by one separatelly.
2-
#
3-
# Executables have no extension, and the same name without extension as the file that originated it:
4-
#
5-
# c.c -> c
6-
# cpp.c -> cpp
7-
# fortran.f -> fortran
8-
#
9-
# Therefore don'e use files with the same basename without extension: e.g. `main.c` and `main.cpp`.
10-
#
11-
# # Most useful invocations
12-
#
13-
# Build all:
14-
#
15-
# make
16-
#
17-
# Build and run output with the default basename:
18-
#
19-
# make run
20-
#
21-
# Build and run output with given basename:
22-
#
23-
# make run c
1+
# `make help` for documentation.
242

253
-include Makefile_params
264

@@ -69,7 +47,7 @@ OUTS_NODIR := $(basename $(INS_NODIR))
6947
OUTS_NODIR := $(addsuffix $(OUT_EXT), $(OUTS_NODIR))
7048
OUTS := $(addprefix $(OUT_DIR), $(OUTS_NODIR))
7149

72-
.PHONY: all asm set_asm_flags clean debug set_debug_flags help mkdir objdump set_objdump_flags profile set_profile_flags $(PHONY)
50+
.PHONY: all asm set_asm_flags clean debug set_debug_flags help mkdir objdump set_objdump_flags profile set_profile_flags test $(PHONY)
7351

7452
all: mkdir $(OUTS)
7553
ifneq ($(strip $(run)),)
@@ -93,7 +71,7 @@ asm: mkdir set_asm_flags
9371
if [ -f "$(ASSEMBLER_NOEXT)$$EXT" ]; then \
9472
case "$$EXT" in \
9573
.c)\
96-
$(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(OPTIMIZE_FLAGS) $(CFLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS)\
74+
$(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(OPTIMIZE_FLAGS) $(CFLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS) -o $(OUT_DIR)asm.o\
9775
;;\
9876
.cpp)\
9977
$(MYCXX) $(MYCXXFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(OPTIMIZE_FLAGS) -c -fverbose-asm -Wa,-adhln "$(ASSEMBLER_NOEXT)$$EXT" $(LIBS)\
@@ -121,7 +99,7 @@ set_debug_flags:
12199
$(eval OPTIMIZE_FLAGS := -O0)
122100

123101
mkdir:
124-
mkdir -p "$(OUT_DIR)"
102+
@mkdir -p "$(OUT_DIR)"
125103

126104
objdump: mkdir set_objdump_flags all
127105
cd $(OUT_DIR) && objdump -S $(RUN_BNAME)
@@ -141,4 +119,45 @@ set_profile_flags:
141119
$(eval PROFILE_DEFINE := -DPROFILE)
142120
#$(eval OPTIMIZE_FLAGS := -O0)
143121

122+
test: all
123+
./test $(OUT_DIR) $(OUTS_NODIR)
124+
144125
-include Makefile_targets
126+
127+
help:
128+
@echo 'Compiles C files with GCC, and C++ files with g++, Fortran files with gfortran one by one separatelly.'
129+
@echo ''
130+
@echo 'Executables have the same name without extension as the file that originated it:'
131+
@echo ''
132+
@echo ' c.c -> c'
133+
@echo ' cpp.c -> cpp'
134+
@echo ' fortran.f -> fortran'
135+
@echo ''
136+
@echo 'Therefore dont use files with the same basename without extension: e.g. `main.c` and `main.cpp`.'
137+
@echo ''
138+
@echo '# Most useful invocations'
139+
@echo ''
140+
@echo 'Build all:'
141+
@echo ''
142+
@echo ' make'
143+
@echo ''
144+
@echo 'Build and run output with the default basename:'
145+
@echo ''
146+
@echo ' make run'
147+
@echo ''
148+
@echo 'Build and run output with given basename:'
149+
@echo ''
150+
@echo ' make run=c'
151+
@echo ''
152+
@echo 'The `=` sign is *not* optional.'
153+
@echo ''
154+
@echo '# Targets'
155+
@echo ''
156+
@echo 'all ................. Build all.'
157+
@echo 'asm [RUN=name] ...... Print generated assembly code for the file with given basename without extension.'
158+
@echo 'clean ............... Clean built files.'
159+
@echo 'debug ............... Run with `gdb`.'
160+
@echo 'help ................ Print help to stdout.'
161+
@echo 'profile ............. Run with `gprof`.'
162+
@echo 'run[=name] .......... Run a file with the given basename withotu extension or the default not given.'
163+
@echo 'test ................ Run `./test <output-directory> <output-basename> ...`'

Makefile_one

-78
This file was deleted.

Makefile_one

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boilerplate/Makefile

README.md

+17-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ C and C++ information, cheatsheets and mini-projects.
22

33
Based on Linux tools, but portable code is clearly separated from non-portable. Thoroughly tested on Ubuntu 12.04. Ports and reproduction reports on other systems are welcome.
44

5-
# Compile and run
5+
# Most useful files
6+
7+
- [c.c](c.c): C cheatsheet.
8+
- [cpp.cpp](main_cpp.cpp): C++ cheatsheet.
9+
- `Makefile.*`: Makefiles that take care of a ton of possibilities.
10+
- [opengl/](opengl/)
11+
- [kde/](kde/)
12+
13+
# Quickstart
614

715
To compile and run each directory on Ubuntu 12.04 do:
816

@@ -21,15 +29,17 @@ run a given file by specifying the basename without extension:
2129
make run=c
2230
make run=cpp
2331

32+
The `=` sign is *not* optional!
33+
2434
Doing just `make run` in those cases will run the default file.
2535

26-
# Most useful files
36+
To print the generated assembly code to the screen use:
2737

28-
- [c.c](c.c): C cheatsheet.
29-
- [cpp.cpp](main_cpp.cpp): C++ cheatsheet.
30-
- `Makefile.*`: Makefiles that take care of a ton of possibilities.
31-
- [opengl/](opengl/)
32-
- [kde/](kde/)
38+
make asm RUN=cpp
39+
40+
To get help on all options use:
41+
42+
make help
3343

3444
# About
3545

boilerplate

Submodule boilerplate added at b85292b

boost/test

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../test_many

c.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,18 @@ int setjmp_func(bool jmp, jmp_buf env_buf)
380380
//#functions
381381

382382
/*
383-
#declaration vs definition
383+
#declaration vs #definition
384384
*/
385385

386386
/*
387387
Declaration can happen many times.
388388
389389
Definition no.
390+
391+
Rationale:
392+
393+
- declaratoin tells the compiler something exists. Its fine to say it exists many times.
394+
- definition determines what code will be stored in programs. There can only be one such code.
390395
*/
391396

392397
void decl_def();

fortran/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../Makefile_many

fortran/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fortran cheatsheet.
2+
3+
Works with the same Makefile as the C one, but put here because people who already have C C++ compilers will want to run without installing anything.
File renamed without changes.

fortran/test

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../test_many

lapack/test

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../test_many

multifile/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/*.elf

multifile/Makefile

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
CFLGS := -Wall
22

3-
.PHONY: all clean run
3+
.PHONY: all clean run
44

55
all: maina.elf mainso.elf mainso_fullpath.elf libab.so
66

77
run: maina.elf mainso.elf mainso_fullpath.elf
88
./maina.elf
9+
# This works only after install target and is the standard production method:
910
#./mainso.elf
10-
#this works only after install target
11-
#and is the standard production method
11+
# This only a test method:
1212
env LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH ./mainso.elf
13-
#this is only a test method
13+
# Full path is stored in the elf.
14+
# Since it starts with './', linker knows it is an absolute path:
1415
./mainso_fullpath.elf
15-
#full path is stored in the elf
16-
#since it starts with './', linker knows it is an abs path
1716

1817
install:
1918
#sudo mv libabso /some/where/in/ld/path/
@@ -33,9 +32,9 @@ mainso.elf: main.o libab.so
3332
#select an specific version such as `libab.so.1`
3433
#env LIBRARY_PATH=$LIBRARY_PATH:. gcc $(CFLGS) main.c -o mainso.elf -lab
3534

36-
#this is not recommended
37-
#better use linker path as in mainso.elf
38-
#readelf -d shows that the ouptut stores the full path
35+
# This is not recommended
36+
# Better use linker path as in mainso.elf
37+
# readelf -d shows that the ouptut stores the full path.
3938
mainso_fullpath.elf: main.o libab.so
4039
gcc $(CFLGS) main.o "$(shell realpath libab.so)" -o mainso_fullpath.elf
4140
#gcc $(CFLGS) main.o -o mainso_fullpath.elf -l"$(shell realpath libab.so)"

multifile/a.c

+17-12
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22

33
#include "a.h"
44

5+
// ERROR already defined in main.
56
//int i = 0;
6-
//ERROR already defined
77

8+
// OK: only visible to this file.
89
static int staticInt = 0;
910

11+
// Define externInt. Will store this initial value on the executable.
1012
int externInt = 0;
1113

14+
// WARN: extern initialized.
15+
// It does not make much sense to add extern to a definition: only to a declaration.
16+
//extern int externIntInt = 1;
17+
18+
// ERROR redefinition:
1219
//void func(){ puts("mainFunc"); }
13-
//ERROR
14-
//redefinition
15-
16-
static void staticFunc(){
17-
printf( "a" );
18-
printf( "%d\n", staticInt );
19-
printf( "%d\n", aHStaticInt );
20-
printf( "%d\n", externInt );
20+
21+
static void staticFunc() {
22+
printf( "a#staticFunc:\n" );
23+
printf( " staticInt = %d\n", staticInt );
24+
printf( " aHStaticInt = %d\n", aHStaticInt );
25+
printf( " externInt = %d\n", externInt );
26+
puts("");
2127
}
2228

23-
void a()
24-
{
25-
//static
29+
void a() {
30+
// Static
2631
staticFunc();
2732
staticInt++;
2833
aHStaticInt++;

0 commit comments

Comments
 (0)