Skip to content

Commit 5be8280

Browse files
committed
Move cheat to top-level.
1 parent 9013d75 commit 5be8280

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+278
-1579
lines changed

Diff for: .gitignore

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
#docs, examples, tests
2-
_gitignore/
3-
4-
#output files: bins/.so/.a
1+
# Output files: bins/.so/.a
52
_out/
63

7-
#intermediate compilation files: .o
4+
# Intermediate compilation files: .o
85
_tmp/
96

10-
#cmake style generated files
7+
# Cmake style generated files
118
build/
129

1310
# Compiled Object files
@@ -25,8 +22,8 @@ build/
2522
*.la
2623
*.a
2724

28-
#assembly code
25+
# Assembly code
2926
*.s
3027

31-
#temporary files
28+
# Temporary files
3229
*.tmp

Diff for: Makefile

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

Diff for: makefile.individual_out renamed to Makefile_many

+34-17
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
#compiles c files with gcc and c++ files with g++, one by one separatelly
2-
3-
#this is mostly for test purposes. see: ``makefile.individual_out_bins`` for a more complete makefile
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
424

5-
-include makefile_params
25+
-include Makefile_params
626

727
# c/cpp
8-
#
928
# Don't use the standard names like `CC` and `CXX` to avoid `?=` getting overridden.
10-
# by the default values.
29+
# by the Makefile default values.
1130
MYCC ?= gcc
1231
MYCXX ?= g++
1332
CFLAGS ?= -std=c11 -Wall -pedantic-errors -march=native $(CFLAGS_EXTRA)
@@ -16,29 +35,28 @@ LIBS ?= -lm #-lGL -lGLU -lglut
1635
INCLUDE_DIRS ?= #-I/usr/include
1736
DEFINES ?= #-D_XOPEN_SOURCE 700
1837

19-
#fortran
38+
# Fortran
2039
FF ?= gfortran
2140
FFLIBS ?= #-l:lapack/liblapack.so
2241
FFLAGS ?= -std=f2003 -Wall -pedantic-errors -march=native $(FFLAGS_EXTRA)
2342

24-
#paths
43+
# Paths
2544
IN_DIR ?= ./
2645
IN_EXTS ?= .c .cpp .f
2746
OUT_DIR ?= _out/
2847
OUT_EXT ?= #.elf
2948

30-
#basename without extension of file to run
49+
# Basename without extension of file to run:
3150
RUN ?= main
3251

3352
ASSEMBLER_NOEXT ?= $(IN_DIR)$(RUN)
34-
3553
DEBUG_DEFINE ?=
3654
DEBUG_FLAGS ?=
3755
PROFILE_DEFINE ?= #-DDEBUG
3856
PROFILE_FLAGS ?= #
3957
OPTIMIZE_FLAGS ?= #-O3
4058

41-
#extra phony targets
59+
# Extra phony targets
4260
PHONY ?=
4361

4462
RUN_BNAME := $(RUN)$(OUT_EXT)
@@ -62,9 +80,9 @@ $(OUT_DIR)%$(OUT_EXT): $(IN_DIR)%.cpp
6280
$(OUT_DIR)%$(OUT_EXT): $(IN_DIR)%.f
6381
$(FF) $(FFLAGS) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(OPTIMIZE_FLAGS) -o "$@" "$<" $(FFLIBS)
6482

65-
#make assembly intermingled with original c code to stdout
66-
#TODO0: how not to rewrite the make rules?
67-
#for bare asm: $(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(OPTIMIZE_FLAGS) $(CFLAGS) -fverbose-asm -S "$(ASSEMBLER_NOEXT)$$EXT"
83+
# Make assembly intermingled with original C code to stdout>
84+
# TODO0: how not to rewrite the make rules?
85+
# For bare asm: $(MYCC) $(PROFILE_DEFINE) $(PROFILE_FLAGS) $(DEBUG_DEFINE) $(DEBUG_FLAGS) $(OPTIMIZE_FLAGS) $(CFLAGS) -fverbose-asm -S "$(ASSEMBLER_NOEXT)$$EXT"
6886
asm: mkdir set_asm_flags
6987
for EXT in $(IN_EXTS); do \
7088
if [ -f "$(ASSEMBLER_NOEXT)$$EXT" ]; then \
@@ -100,7 +118,6 @@ set_debug_flags:
100118
mkdir:
101119
mkdir -p "$(OUT_DIR)"
102120

103-
#
104121
objdump: mkdir set_objdump_flags all
105122
cd $(OUT_DIR) && objdump -S $(RUN_BNAME)
106123

@@ -112,11 +129,11 @@ profile: clean set_profile_flags all run
112129
cd $(OUT_DIR) && gprof -b $(RUN_BNAME) gmon.out | tee "$(RUN_BNAME).profile_out" | less
113130

114131
run: all
115-
cd $(OUT_DIR) && ./$(RUN_BNAME) #run from out dir
132+
cd $(OUT_DIR) && ./$(RUN_BNAME)
116133

117134
set_profile_flags:
118135
$(eval PROFILE_FLAGS := -p -pg)
119136
$(eval PROFILE_DEFINE := -DPROFILE)
120137
#$(eval OPTIMIZE_FLAGS := -O0)
121138

122-
-include makefile_targets
139+
-include Makefile_targets

Diff for: makefile.single_out renamed to Makefile_one

+13-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
#compiles all c or cpp files in current dir into a single output file
1+
# Compiles all C or CPP files in current dir into a single output file.
2+
3+
-include Makefile_params
24

35
ASSEMBLE_BASENAME ?= a.c #basename of the file to generate assembly code for
46
AUX_DIR ?= _aux/
@@ -52,42 +54,25 @@ clean:
5254
debug: clean set_debug_flags all
5355
gdb $(OUT)
5456

55-
set_debug_flags:
56-
$(eval DEBUG_FLAGS := -ggdb3)
57-
$(eval DEBUG_DEFINE := -DDEBUG)
58-
$(eval OPTIMIZE_FLAGS := -O0)
57+
mkdir:
58+
mkdir -p "$(AUX_DIR)"
59+
mkdir -p "$(OUT_DIR)"
5960

6061
profile: clean set_profile_flags all run
6162
mv -f gmon.out "$(OUT_DIR)"
6263
gprof -b $(OUT) "$(OUT_DIR)"gmon.out | tee "$(OUT).profile_out" | less
6364

65+
set_debug_flags:
66+
$(eval DEBUG_FLAGS := -ggdb3)
67+
$(eval DEBUG_DEFINE := -DDEBUG)
68+
$(eval OPTIMIZE_FLAGS := -O0)
69+
6470
set_profile_flags:
6571
$(eval PROFILE_FLAGS := -p -pg)
6672
$(eval PROFILE_DEFINE := -DPROFILE)
6773
#$(eval OPTIMIZE_FLAGS := )
6874

6975
run: all
70-
cd $(OUT_DIR) && ./$(OUT_BASENAME) $(RUN_ARGS) #run from out dir
71-
72-
mkdir:
73-
mkdir -p "$(AUX_DIR)"
74-
mkdir -p "$(OUT_DIR)"
76+
cd $(OUT_DIR) && ./$(OUT_BASENAME) $(RUN_ARGS)
7577

76-
ubuntu_install_deps:
77-
#sudo aptitude install -y libgtk-3-dev
78-
79-
help:
80-
@echo 'sample invocations:'
81-
@echo ' ubuntu_install_deps'
82-
@echo ' #installs dependencies on Ubuntu'
83-
@echo ' make'
84-
@echo ' make clean'
85-
@echo ' make run'
86-
@echo ' #makes then runs output'
87-
@echo ' make debug'
88-
@echo ' #makes with -DDEBUG and -ggdb3'
89-
@echo ' #and runs with gdb -b'
90-
@echo ' make profile'
91-
@echo ' #clean, make with -DPROFILE and -p -pg'
92-
@echo ' #runs and pipes output through less'
93-
@echo ' #and saves compile output to <outname>.profile_out'
78+
-include Makefile_targets

Diff for: c/makefile_params renamed to Makefile_params

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ CFLAGS_EXTRA := -Wno-overflow -Wno-sequence-point -Wno-unused-variable -Wno-unus
22
CXXFLAGS_EXTRA := -Wno-sequence-point -Wno-unused-variable -Wno-unused-but-set-variable -pthread
33
FFLAGS_EXTRA := -Wno-unused-variable -Wno-unused-but-set-variable
44
RUN := c
5-
PHONY := install-deps-ubuntu
5+
PHONY := install-ubuntu

Diff for: Makefile_targets

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
install-ubuntu:
2+
sudo apt-get update
3+
sudo apt-get install aptitude
4+
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
5+
sudo aptitude update
6+
# C
7+
sudo aptitude install -y gcc-4.8
8+
sudo aptitude install -y gcc-4.8-doc
9+
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
10+
# C++
11+
sudo aptitude install -y g++-4.8
12+
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
13+
# Fortran
14+
sudo aptitude install -y gfortran

Diff for: README.md

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,37 @@
1-
Simple C and C++ programs, cheats and mini projects.
1+
C and C++ information, cheatsheets and mini-projects.
22

3-
The most useful sections are currently:
3+
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-
- [c/c.c](c/c.c): C cheatsheet.
6-
- [c/cpp.cpp](c/cpp.cpp): C++ cheatsheet.
7-
- `makefile.*`: makefiles that take care of a ton of possibilities.
5+
# Compile and run
86

9-
Other reasonably useful cheats can be found under:
7+
To compile and run each directory on Ubuntu 12.04 do:
108

11-
- [opengl/](opengl/)
12-
- [kde/](kde/)
9+
make install-ubuntu
10+
make run
1311

14-
# Install on Linux
12+
You *must* run `make install-ubuntu` on the top-level before compiling any subdirectories.
1513

16-
Most systems already have GCC installed, however some of this relies on the recent GCC 4.7 to be able to try out bleeding edge c11.
14+
When there are multiple files compiled, e.g.:
1715

18-
This is not recommended for production.
16+
c.c -> c
17+
cpp.cpp -> cpp
1918

20-
On Ubuntu 12.04, install GCC 4.8 with:
19+
run a given file by specifying the basename without extension:
2120

22-
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
23-
sudo aptitude install -y update
24-
sudo aptitude install gcc-4.8
25-
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
21+
make run RUN=c
22+
make run RUN=cpp
2623

27-
# About
24+
Doing just `make run` in those cases will run the default file.
25+
26+
# Most useful files
2827

29-
Currently uses Linux tools, but Windows ports are welcome.
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/)
33+
34+
# About
3035

3136
Larger projects may be in separate dirs.
3237

Diff for: c/algorithm.cpp renamed to algorithm.cpp

File renamed without changes.

Diff for: boost/Makefile

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

Diff for: boost/Makefile_params

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
LIBS := -lboost_graph -lm
2+
PHONY := install-ubuntu

Diff for: boost/Makefile_targets

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
install-ubuntu:
2+
sudo aptitude update
3+
sudo aptitude install -y libboost1.48-all-dev

Diff for: boost/graph.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ main(int, char *[])
6262

6363
graph_traits < graph_t >::edge_iterator ei, ei_end;
6464
for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
65-
graph_traits < graph_t >::edge_descriptor e = *ei;
65+
edge_descriptor e = *ei;
6666
graph_traits < graph_t >::vertex_descriptor
6767
u = source(e, g), v = target(e, g);
6868
dot_file << name[u] << " -> " << name[v]

Diff for: boost/makefile

-1
This file was deleted.

Diff for: boost/makefile_params

-2
This file was deleted.

Diff for: boost/makefile_targets

-4
This file was deleted.

Diff for: c/c.c renamed to c.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -6530,11 +6530,11 @@ int main(int argc, char **argv)
65306530
sprintf(s, "%-6s", "abc");
65316531
assert(strcmp(s, "abc ") == 0);
65326532

6533-
// Does not work with zeros:
6533+
// Does not work with zeros. gcc 4.8.1 gives a warning.
65346534

6535-
sprintf(s, "%-06s", "abc");
6536-
/*printf("%s\n", s);*/
6537-
/*assert(strcmp(s, "abc ") == 0);*/
6535+
// sprintf(s, "%-06s", "abc");
6536+
// printf("%s\n", s);
6537+
// assert(strcmp(s, "abc ") == 0);
65386538
}
65396539
}
65406540

@@ -8492,7 +8492,7 @@ int main(int argc, char **argv)
84928492
C99: return is optional. If omited a `return 0` is added to the program.
84938493
*/
84948494

8495-
//main returns status:
8495+
// Main returns status:
84968496

84978497
return EXIT_SUCCESS;
84988498
return EXIT_FAILURE;

Diff for: c/README.md

-5
This file was deleted.

Diff for: c/makefile

-1
This file was deleted.

Diff for: c/so.vim

-1
This file was deleted.

Diff for: c/cpp.cpp renamed to cpp.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -9133,11 +9133,11 @@ int main(int argc, char **argv)
91339133
#if __cplusplus >= 201103L
91349134

91359135
/*
9136-
#thread
9136+
#thread #multithreading #parallel #concurrency
91379137
91389138
c++11
91399139
9140-
needs `-pthread` flag on g++ linux
9140+
Needs `-pthread` flag on g++ Linux.
91419141
*/
91429142
{
91439143

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)