Skip to content

Commit 0d20028

Browse files
committed
Start removing dependency on boilerplate
1 parent 298b8b1 commit 0d20028

29 files changed

+636
-566
lines changed

.vimrc

Lines changed: 0 additions & 1 deletion
This file was deleted.

.vimrc_many

Lines changed: 0 additions & 30 deletions
This file was deleted.

.vimrc_one

Lines changed: 0 additions & 1 deletion
This file was deleted.

README.md

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,44 @@
44

55
C and C++ information, cheatsheets and mini-projects.
66

7-
There may be other compiled languages here for which we don't have much material for a separate repository, specially when we interface that language with C. E.g.: Fortran.
8-
97
Relies on <https://github.com/cirosantilli/cpp-boilerplate> to factor code out. See [its documentation](https://github.com/cirosantilli/cpp-boilerplate/blob/master/README.md) for information on how to use this project.
108

119
## Most useful files
1210

13-
- [c.c](c.c): C cheatsheet.
14-
- [cpp.cpp](main_cpp.cpp): C++ cheatsheet.
15-
- [posix](posix/): POSIX C API.
11+
- [c.c](c.c): C cheatsheet
12+
- [cpp.cpp](main_cpp.cpp): C++ cheatsheet
13+
- [posix](posix/): POSIX C API
14+
- [multifile/](multifile/): `include`, `extern` vs `static`, dynamic libraries, cross language calls
15+
- [gcc/](gcc/): GCC extensions
1616
- [opengl/](opengl/)
1717
- [kde/](kde/)
1818

19-
## Quickstart
19+
Less useful files:
20+
21+
- [cmake](cmake.md)
22+
- [fortran/](fortran/)
23+
- [hello_world.c](hello_world.c)
24+
- [hello_world.c](hello_world.c)
25+
26+
## Dependencies
27+
28+
Most builds require:
29+
30+
- `make` (POSIX)
31+
- `gcc` >= 4.7
32+
- `g++` >= 4.7
33+
34+
Even though we use GNU tools by default, great attention is paid to portability of portable subjects like ANSI C, which should compile in any compiler.
35+
36+
In addition, each directory may have their own extra dependencies as stated in their README.
2037

21-
On Ubuntu 12.04:
38+
You can install dependencies on latest LTS Ubuntus with:
2239

2340
./configure
24-
make run
41+
42+
Other system install are not going to be supported as that would lead to too much maintenance overhead. If you don't have Ubuntu, consider using our [Vagrantfile](Vagrantfile).
43+
44+
## Usage
2545

2646
When there are multiple files compiled, e.g.:
2747

@@ -35,15 +55,7 @@ run a given file by specifying the basename without extension:
3555

3656
The `=` sign is *not* optional!
3757

38-
Doing just `make run` in those cases will run the default file.
39-
40-
To print the generated assembly code to the screen use:
41-
42-
make asm RUN=cpp
43-
44-
To get help on all options use:
45-
46-
make help
58+
Doing just `make run` in those cases will run the default file: `main`.
4759

4860
## Style
4961

c.c

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ int setjmp_func(int jmp, jmp_buf env_buf) {
383383
return 1;
384384
}
385385

386-
/* # Functions */
386+
/* Functions */
387387

388388
/*
389-
# declaration vs #definition
389+
#Declaration vs #definition
390390
*/
391391

392392
/*
@@ -678,6 +678,15 @@ int setjmp_func(int jmp, jmp_buf env_buf) {
678678
*j += *add;
679679
}
680680

681+
/*
682+
This declaration is required!
683+
684+
- http://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99
685+
- http://stackoverflow.com/questions/12747198/compiling-error-when-std-gnu99-and-inline-function-is-used
686+
*/
687+
int inline_func(int i);
688+
inline int inline_func(int i) { return i + 1; }
689+
681690
#if __STDC_VERSION__ >= 199901L
682691
int static_array_argument(int is[static 3]) {
683692
return is[3];
@@ -2676,27 +2685,6 @@ int main(int argc, char **argv) {
26762685
}
26772686
}
26782687

2679-
/*
2680-
# inline keyword
2681-
2682-
Signals the compiler that it may be worth to copy paste the function instead of calling it.
2683-
2684-
The compiler is not obliged
2685-
2686-
effects:
2687-
2688-
- avoids function call, thus potentially faster
2689-
- code gets larger
2690-
- function pointer comparisons may differ for the same function
2691-
- instruction cache might be come less efficient making thigs slower
2692-
2693-
sources:
2694-
2695-
- <http://www.greenend.org.uk/rjk/tech/inline.html>
2696-
2697-
some warnings about inline and its usage
2698-
*/
2699-
27002688
/*
27012689
# typedef
27022690
@@ -5405,7 +5393,7 @@ int main(int argc, char **argv) {
54055393
*/
54065394
{
54075395
int is[] = {0, 1, 2};
5408-
for (int i = 0, j = 10; i < 20; ++i, ++j) {
5396+
for (int i = 0, j = 0; j < 30; ++i, j += 10) {
54095397
assert(i == is[i]);
54105398

54115399
/* ERROR: redeclaration. */
@@ -5868,6 +5856,30 @@ int main(int argc, char **argv) {
58685856
}
58695857
#endif
58705858

5859+
/*
5860+
# inline keyword
5861+
5862+
Signals the compiler that it may be worth to copy paste the function instead of calling it.
5863+
5864+
The compiler is not obliged
5865+
5866+
effects:
5867+
5868+
- avoids function call, thus potentially faster
5869+
- code gets larger
5870+
- function pointer comparisons may differ for the same function
5871+
- instruction cache might be come less efficient making thigs slower
5872+
5873+
Sources:
5874+
5875+
- <http://www.greenend.org.uk/rjk/tech/inline.html>
5876+
5877+
Some warnings about inline and its usage.
5878+
*/
5879+
{
5880+
assert(inline_func(0) == 1);
5881+
}
5882+
58715883
}
58725884

58735885
/*
@@ -7356,10 +7368,15 @@ int main(int argc, char **argv) {
73567368
Modes:
73577369
73587370
- `r`: read. compatible with a,w
7371+
73597372
- `w`: read and write. destroy if exists, create if not.
7373+
73607374
- `a`: append. write to the end. creates if does not exist.
7375+
73617376
- `+`: can do both input and output. msut use flush or fseek
7377+
73627378
- `x`: don't destroy if exist (c11, not c++!)
7379+
73637380
- `b`: binary.
73647381
73657382
Means nothing in POSIX systems.

cmake.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# CMake
2+
13
Make alternative that aims to be:
24

35
- more cross platform: works on Linux and Windows

configure

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
11
#!/usr/bin/env bash
2-
# This cannot be put into makefiles because some systems don't have make installed.
3-
set -e
4-
distro_id="$(lsb_release -i | sed -r 's/.*:\t(.*)/\1/')"
5-
distro_version="$(lsb_release -r | sed -r 's/.*:\t(.*)/\1/')"
6-
if [ "$distro_id" = "Ubuntu" ]; then
7-
sudo apt-get update
8-
sudo apt-get install -y aptitude
9-
sudo aptitude install -y build-essential
10-
# C and C++
11-
sudo aptitude install -y python-software-properties
12-
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
13-
sudo aptitude update
14-
sudo aptitude install -y gcc-4.8
15-
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
16-
sudo aptitude install -y g++-4.8
17-
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
18-
# Fortran
19-
sudo aptitude install -y gfortran-4.8
20-
elif [ "$distro_id" = "Debian" ]; then
21-
sudo apt-get update
22-
sudo apt-get install aptitude
23-
sudo aptitude install -y build-essential
24-
sudo aptitude install -y g++
25-
else
26-
echo 'Automatic dependency installation is not supported on your system.'
27-
printf 'Dependencies are:
28-
gcc >= 4.7
29-
gfortran >= 4.7
30-
g++ >= 4.7
31-
make
32-
'
33-
exit 1
34-
fi
2+
# This cannot be put into makefiles because
3+
# some systems don't have make installed.
4+
sudo apt-get update
5+
sudo apt-get install -y aptitude
6+
sudo aptitude install -y build-essential
7+
# C and C++
8+
sudo aptitude install -y python-software-properties
9+
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
10+
sudo aptitude update
11+
sudo aptitude install -y gcc-4.8
12+
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
13+
sudo aptitude install -y g++-4.8
14+
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50
15+
# Fortran
16+
sudo aptitude install -y gfortran-4.8

0 commit comments

Comments
 (0)