├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── LICENSE ├── Makefile_many ├── Makefile_one ├── Makefile_params ├── Makefile_targets ├── README.md ├── Vagrantfile ├── boost ├── Makefile ├── Makefile_params ├── README.md ├── box.cpp ├── common.hpp ├── configure ├── dijkstra.dot ├── geometry.md ├── graph.cpp ├── main.cpp ├── point.cpp └── rtree.cpp ├── bullet ├── .gitignore ├── Makefile ├── Makefile_params ├── Makefile_targets ├── README.md ├── TODO.md ├── alternatives.md ├── benchmarks.md ├── bibliography.md ├── build.md ├── clean ├── common.hpp ├── concepts.md ├── configure ├── getting-started.md ├── gravity.cpp ├── gravity_collision.cpp ├── gravity_collision.py ├── introduction.md ├── plot ├── requirements.txt ├── source-code.md ├── source-tree.md ├── two_objects.cpp ├── users.md └── which_collision.cpp ├── c ├── Makefile ├── Makefile_params ├── README.md ├── abs.c ├── array.c ├── array_designated_initializer.c ├── assert_h.c ├── atexit.c ├── auto.c ├── basic_type.md ├── bibliography.md ├── bitfield.c ├── comments.c ├── common.h ├── complex_h.c ├── compound_literal.c ├── compound_struct_literal.c ├── const.c ├── constant_expression.c ├── ctype_h.c ├── design_patterns.c ├── enum.c ├── errno_h.c ├── exit.c ├── extern │ ├── Makefile │ ├── README.md │ ├── a.c │ └── main.c ├── file_write_read_numeric.c ├── flexible_array_member.c ├── float32.h ├── float_h.c ├── for.c ├── function.c ├── function_array_argument.c ├── function_pointer.c ├── generic.c ├── getenv.c ├── global.c ├── goto.c ├── hello_world.c ├── hex_float.c ├── identifier.c ├── identifier_list.c ├── if.c ├── implementations.md ├── incomplete_type.c ├── inline.c ├── interactive │ ├── Makefile │ ├── README.md │ ├── audio_gen.c │ ├── branch_prediction_and_patterns_perf.c │ ├── clock.c │ ├── command_line_arguments.c │ ├── common.h │ ├── float_range.c │ ├── how-to-programmatically-synthesize-music.md │ ├── profiling.c │ ├── subnormal.c │ ├── tic_tac_toe.c │ ├── ugly_grammar.c │ └── user_input.c ├── inttypes_h.c ├── iso646_h.c ├── lexer.c ├── libraries.md ├── limits_h.c ├── literals.c ├── main_function.c ├── malloc.c ├── math_h.c ├── memory_layout.c ├── min.c ├── nan.c ├── ndebug.c ├── no_return_non_void.c ├── noreturn.c ├── null.c ├── offsetof.c ├── operator.c ├── parameter_without_name.c ├── perror.c ├── pointer.c ├── pointer_to_int.c ├── polymorphism.c ├── preprocessor.c ├── preprocessor.inc ├── preprocessor.sh ├── printf.c ├── pros-and-cons.md ├── ptrdiff_t.c ├── rand.c ├── register.c ├── restrict.c ├── sequence_point.c ├── setjmp_h.c ├── signal.c ├── sizeof.c ├── standards.md ├── static │ ├── Makefile │ ├── README.md │ ├── function │ │ ├── Makefile │ │ ├── README.md │ │ ├── main.c │ │ └── notmain.c │ └── variable │ │ ├── Makefile │ │ ├── README.md │ │ ├── main.c │ │ ├── notmain.c │ │ └── notmain.h ├── static_array_argument.c ├── static_assert.c ├── static_local.c ├── stdbool_h.c ├── stdint_h.c ├── stdio_h.c ├── stdlib_h.c ├── str2num.c ├── str2num.h ├── strftime.c ├── string.c ├── string_h.c ├── struct.c ├── struct_designated_initializer.c ├── struct_operators.c ├── style-guides.md ├── switch.c ├── template.c ├── template_cpp.c ├── template_large.c ├── time_h.c ├── trigraph.c ├── trivia.md ├── type_qualifier.c ├── typecast.c ├── typedef.c ├── typedef_function.c ├── types.c ├── undefined-behaviour.md ├── uninitialized_variable.c ├── union.c ├── varargs_h.c ├── vla.c ├── void.c ├── void_p ├── void_parameter.c ├── void_pointer.c ├── volatile.c ├── wchar_h.c └── while.c ├── c_from_cpp ├── Makefile ├── README.md ├── main.cpp ├── notmain.c └── notmain.h ├── cmake ├── README.md ├── hello │ ├── CMakeLists.txt │ └── main.c ├── multi_executable │ ├── CMakeLists.txt │ ├── a.c │ └── b.c ├── multi_file │ ├── CMakeLists.txt │ ├── a.c │ ├── a.h │ └── main.c ├── multi_file_recursive │ ├── CMakeLists.txt │ └── src │ │ ├── d │ │ ├── a.c │ │ └── a.h │ │ └── main.c └── shared_lib_external │ ├── CMakeLists.txt │ └── main.c ├── configure ├── cpp ├── Makefile ├── Makefile_params ├── README.md ├── TODO.md ├── aggregate.cpp ├── algorithm.cpp ├── array.cpp ├── attributes.cpp ├── auto.cpp ├── bitset.cpp ├── bool.cpp ├── cast_operator.cpp ├── class.cpp ├── common.hpp ├── const.cpp ├── const_cast.cpp ├── constexpr.cpp ├── constructor_init_object_member.cpp ├── containers.md ├── covariant_return.cpp ├── crtp.cpp ├── default_parameters.cpp ├── delegating_constructor.cpp ├── deque.cpp ├── design_patterns.cpp ├── enum.cpp ├── enum_signed.cpp ├── exception.cpp ├── explicit.cpp ├── extern_const │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── main.cpp │ ├── notmain.cpp │ └── notmain.hpp ├── file_io.cpp ├── file_to_memory.cpp ├── func_ret_maybe_static.cpp ├── function.cpp ├── functional.cpp ├── goto_cross_initialization.cpp ├── heap.cpp ├── hello_world.cpp ├── implicitly_defined.cpp ├── implicitly_deleted_constructor.cpp ├── inherit_overload_hide.cpp ├── inheritance_and_constructors.cpp ├── inheritance_override.cpp ├── inheriting_constructor.cpp ├── inline_variable │ ├── Makefile │ ├── README.md │ ├── main.cpp │ ├── notmain.cpp │ └── notmain.hpp ├── interactive │ ├── Makefile │ ├── chrono.cpp │ └── common.hpp ├── iostream.cpp ├── iterator.cpp ├── lambda.cpp ├── limits.cpp ├── list.cpp ├── main.cpp ├── method_pointer.cpp ├── move.cpp ├── namespace.cpp ├── nan.cpp ├── new.cpp ├── nullptr.cpp ├── operator_overload.cpp ├── operators.cpp ├── overload.cpp ├── override.cpp ├── pair.cpp ├── parameter_without_name.cpp ├── pod.cpp ├── preprocessor.cpp ├── pure_virtual.cpp ├── pure_virtual_from_constructor.cpp ├── range_switch_case.cpp ├── raw_string_literal.cpp ├── reference.cpp ├── regex.cpp ├── rtti.md ├── rvalue_reference.cpp ├── set.cpp ├── shared_ptr.cpp ├── sstream.cpp ├── standard_library.md ├── standards.md ├── static.cpp ├── static_assert.cpp ├── static_cast.cpp ├── static_constructor_private.cpp ├── std_move.cpp ├── string.cpp ├── struct.cpp ├── style-guides.md ├── template_instantiation │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── main.cpp │ ├── notmain.cpp │ └── notmain.hpp ├── template_on_header │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── main.cpp │ ├── mytemplate.hpp │ ├── notmain.cpp │ └── notmain.hpp ├── trailing_return_type.cpp ├── trivia.md ├── tuple.cpp ├── typecast.cpp ├── typedef.cpp ├── typeid.cpp ├── unique_ptr.cpp ├── unordered_set.cpp ├── valarray.cpp ├── value_category.cpp ├── vector.cpp └── virtual_inheritance.cpp ├── cpp_from_c ├── Makefile ├── README.md ├── main.c ├── notmain.cpp └── notmain.hpp ├── cuda ├── Makefile ├── README.md ├── c_cu │ ├── README.md │ ├── inc.cu │ └── main.cpp └── inc.cu ├── eigen ├── Makefile ├── Makefile_params ├── README.md ├── configure └── svd.cpp ├── ffmpeg ├── .gitignore ├── Makefile ├── Makefile_params ├── README.md ├── TODO.md ├── architecture.md ├── bibliography.md ├── build.md ├── common.h ├── configure ├── encode.c ├── introduction.md ├── libav.md ├── resize.c ├── source-tree.md └── trivia.md ├── flex_bison ├── README.md ├── bison │ ├── Makefile │ ├── README.md │ ├── compiler.c.off │ ├── graphic_tree.c.off │ ├── h.h │ ├── interpreter.c │ ├── l.l │ ├── test │ ├── test_io.py │ └── y.y ├── configure ├── cpp │ ├── Makefile │ ├── README.md │ ├── bison.y │ ├── flex.l │ ├── heading.h │ ├── main.cpp │ ├── test │ └── test_io.py ├── flex │ ├── Makefile │ ├── README.md │ ├── flex.lex │ ├── test │ └── test_io.py └── test ├── fortran ├── Makefile ├── README.md ├── configure └── main.f ├── fortran_from_c ├── Makefile ├── README.md ├── f.f ├── f.h └── main.c ├── gcc ├── 128_bit_int.c ├── Makefile ├── Makefile_params ├── README.md ├── ada │ ├── .gitignore │ ├── Makefile │ ├── README.md │ └── main.adb ├── alloca.c ├── array_range_initialization.c ├── asm.c ├── asm.md ├── atomic_fetch_add.c ├── attribute.c ├── auto_type.c ├── bibliography.md ├── build.md ├── built-in.md ├── builtin_constant_p.c ├── builtin_expect.c ├── builtin_prefetch.c ├── builtin_return_address.c ├── case_range.c ├── common.h ├── compilation-passes.md ├── compilation-steps.md ├── complex_integer.c ├── cpp-invocation.md ├── cpp.c ├── cross-compiler.md ├── empty_struct.c ├── gcc-as-library.md ├── go │ ├── Makefile │ ├── README.md │ └── main.go ├── ide.md ├── identifiers.c ├── introduction.md ├── invocation.md ├── libgcc.md ├── literals.c ├── local_label.c ├── nested_functions.c ├── sentinel.c ├── source-tree.md ├── spec-files.md ├── statement_expression.c ├── typeof.c ├── vector.c ├── weak │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── main.c │ └── notmain.c └── zero_length_array.c ├── gcc_std_default ├── Makefile ├── README.md ├── c.c ├── c.sh ├── cpp.cpp └── cpp.sh ├── gdb ├── Makefile ├── README.md ├── arg_stdin.c ├── argv-wrapper ├── argv.py ├── array.c ├── array.gdb ├── big_function.c ├── block.py ├── block_py.c ├── break_return.py ├── breakpoint.py ├── breakpoint_silent.py ├── call-graph.md ├── call.c ├── call_graph_py.c ├── call_graph_rbreak.py ├── call_graph_rbreak_directory.py ├── call_graph_return.py ├── call_graph_walk.py ├── call_graph_walk_filter.py ├── command.py ├── commands.md ├── common.py ├── compile.c ├── compile.gdb ├── container.cpp ├── continue_instruction.py ├── continue_return.py ├── continue_return_py.c ├── count_infinite.c ├── count_infinite_test.py ├── define.c ├── define.gdb ├── disas.py ├── disas_py.c ├── disassemble.py ├── dwarf.md ├── empty.c ├── event.py ├── finish_breakpoint.py ├── finish_breakpoint_py.c ├── fork.c ├── fork.gdb ├── frame.py ├── function_pointer.c ├── function_pointer.gdb ├── getting-started.md ├── globals.py ├── hello_world.c ├── hello_world.py ├── hello_world_py.gdb ├── help.py ├── if_else.c ├── internals.md ├── introduction.md ├── invocation.md ├── layout.md ├── many_lines.c ├── maybe_crash.c ├── maybe_sleep_forever.c ├── method.cpp ├── method.gdb ├── multifile │ ├── Makefile │ ├── README.md │ ├── a.c │ ├── d │ │ ├── a.c │ │ └── b.c │ ├── e │ │ └── a.c │ └── main.c ├── multiple_python_invocations.gdb ├── nested.c ├── nexti.c ├── overload.cpp ├── polymorphism.cpp ├── print_lines.py ├── python-interface.md ├── rbreak_directory.py ├── read_var.py ├── read_var_py.c ├── requirements.txt ├── reverse.c ├── reverse.gdb ├── run ├── segv.c ├── si_all.gdb ├── sleep.c ├── smart_pointer.cpp ├── stderr_ignore.c ├── stderr_ignore.gdb ├── stdout.c ├── stdout.gdb ├── step_all.gdb ├── string.c ├── string.gdb ├── struct.c ├── struct.gdb ├── test ├── threads.c ├── threads.md ├── trace.c ├── trace.gdb ├── trace.md ├── value.py ├── visible_variables.py ├── visible_variables_py.c ├── watch.c └── watch.gdb ├── getting-started.md ├── glibc ├── Makefile ├── README.md ├── acct.c ├── brk.c ├── clone.c.off ├── common.h ├── crt1.md ├── gnu_get_libc_version.c ├── interactive │ ├── Makefile │ ├── common.h │ ├── feenableexcept.cpp │ ├── map_anonymous_semaphore.c │ ├── o_largefile.c │ └── sendfile.c ├── introduction.md ├── libc_main.c ├── makefile.md ├── ptrace.c ├── source-tree.md ├── strfry.c ├── strverscmp.c └── syscall.c ├── gsl ├── .gitignore ├── Makefile ├── Makefile_params ├── README.md ├── bessel.c ├── common.h ├── configure ├── ode.c ├── ode.gnuplot └── ode.md ├── gtk ├── Makefile ├── Makefile_params ├── builder.ui ├── configure ├── draw.c ├── min.c ├── packing.c └── xml.c ├── kde ├── CMakeLists.txt ├── Makefile ├── README.md ├── configure ├── general.ui ├── kdecheat.kcfg ├── kdecheatui.rc ├── main.cpp ├── main.h └── settings.kcfgc ├── lapack ├── Makefile ├── Makefile_params ├── README.md ├── c.c ├── configure ├── main.f └── test ├── linker ├── gold_benchmark │ ├── .gitignore │ ├── README.md │ ├── bench │ ├── bench-all │ ├── clean │ ├── clean-build │ └── generate-objects ├── incremental_link │ ├── Makefile │ ├── README.md │ ├── f1.c │ ├── f1.h │ ├── f12.c │ ├── f2.c │ ├── f2.h │ └── main.c └── variable_address │ ├── Makefile │ ├── README.md │ ├── link.ld │ └── main.c ├── linux ├── Makefile ├── README.md ├── configure └── fb.c ├── make ├── .gitignore ├── Makefile ├── automatic-variables.mk ├── bibliography.md ├── builtin_functions │ ├── Makefile │ └── README.md ├── default_targets │ ├── .gitignore │ ├── README.md │ ├── c.c │ ├── clean.sh │ ├── cpp.cpp │ └── make.sh ├── directives │ └── conditional │ │ └── Makefile ├── environment-variables.mk ├── include │ ├── Makefile │ ├── a.makefile │ └── b.makefile ├── indent.mk ├── introduction.md ├── invocation.md ├── multifile │ ├── Makefile │ ├── README.md │ └── d │ │ └── Makefile ├── multiline-variables.mk ├── plus_sign │ ├── Makefile │ └── test ├── precious │ ├── Makefile │ └── README.md ├── recipes │ ├── Makefile │ ├── README.md │ ├── interpreter │ │ ├── Makefile │ │ └── README.md │ └── test ├── rule │ ├── Makefile │ ├── README.md │ ├── redefine │ │ ├── Makefile │ │ └── README.md │ └── timestamp │ │ ├── .gitignore │ │ ├── Makefile │ │ ├── README.md │ │ └── test.sh ├── target-specific-variable.mk ├── template.mk ├── templates │ ├── README.md │ ├── c_many │ │ ├── .gitignore │ │ ├── Makefile │ │ ├── a.c │ │ ├── main.c │ │ └── test │ ├── c_one │ │ ├── .gitignore │ │ ├── Makefile │ │ ├── a.c │ │ └── main.c │ ├── c_single │ │ ├── .gitignore │ │ ├── Makefile │ │ └── main.c │ ├── fortran-one.makefile │ ├── java │ │ ├── .gitignore │ │ ├── B.java │ │ ├── C.java │ │ ├── Main.java │ │ ├── Makefile │ │ └── README.md │ ├── nasm-many.makefile │ ├── nasm-single-dry.makefile │ ├── nasm-single.makefile │ └── toplevel_reuse │ │ ├── Makefile │ │ ├── README.md │ │ ├── a │ │ ├── Makefile │ │ └── f │ │ ├── b │ │ ├── Makefile │ │ └── f │ │ ├── f │ │ ├── g │ │ └── proxy.makefile ├── test ├── variables.mk ├── verbose.md └── verbose.mk ├── opencl ├── Makefile ├── Makefile_params ├── README.md ├── TODO.md ├── alternatives.md ├── applications.md ├── architecture.md ├── benchmark-examples.md ├── benchmarks.md ├── bibliography.md ├── binary_shader.c ├── c.md ├── clinfo.c ├── clinfo.md ├── common.h ├── configure ├── getting-started.md ├── host-api.md ├── implementations.md ├── inc.c ├── inc.cl ├── inc_vector.c ├── inc_vector_globals.c ├── interactive │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── common.h │ ├── matmul.c │ ├── matmul.cl │ ├── matmul_block.cl │ ├── matmul_row_local.cl │ ├── matmul_row_priv.cl │ ├── matmul_row_priv_col_local.cl │ ├── matmul_row_priv_cols_local.cl │ ├── vec_io.c │ ├── vec_io.cl │ ├── vec_io.vec │ └── vec_io_sum.cl ├── introduction.md ├── multi_kernel.c ├── pass_by_value.c ├── preprocessor.c ├── vector_type.c ├── work_item_builtin.c ├── work_item_builtin.cl └── write_buffer.c ├── opencv ├── Makefile ├── Makefile_params ├── README.md ├── configure ├── input │ └── sky.jpg └── main.cpp ├── opengl ├── .gitignore ├── Makefile ├── Makefile_params ├── README.md ├── TODO.md ├── about.md ├── ambient.c ├── benchmarks.md ├── bibliography.md ├── bitmap_character.c ├── bouncing_balls_many.cpp ├── bouncing_balls_one.cpp ├── common.h ├── common.hpp ├── common_glut.h ├── common_texture.h ├── compute-shader.md ├── configure ├── culling.c ├── depth.c ├── diffuse_attenuation.c ├── diffuse_infinity.c ├── diffuse_shadow.c ├── draw_pixels.c ├── drivers.md ├── egl.md ├── gles │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── introduction.md │ └── triangle.c ├── glfw.md ├── glfw_color_array.c ├── glfw_compute_shader.c ├── glfw_compute_shader_heat_equation.c ├── glfw_compute_shader_ssbo_inc.c ├── glfw_compute_shader_wave_equation.c.off ├── glfw_gl_frag_coord.c ├── glfw_heatmap.c ├── glfw_hello_world.c ├── glfw_ibo.c ├── glfw_infinite_loop_shader.c.off ├── glfw_memory_overflow_shader.c.off ├── glfw_recursive.c.off ├── glfw_texture.c ├── glfw_transform.c ├── glfw_triangle_3d.c ├── glfw_triangle_no_vbo.c ├── glfw_triangle_rotate.c ├── glfw_triangle_vao.c ├── glfw_triangle_vbo.c ├── glfw_triangles.c ├── glfw_webcam_image_process.c ├── glsl.md ├── glut.md ├── glx.c ├── grass.jpg ├── hello_world.c ├── info.c ├── introduction.md ├── lightning.md ├── look_at.c ├── main.cpp ├── many_triangles_animation.c ├── misc.md ├── mouse_getpixel.cpp ├── normal.c ├── offscreen.c ├── ortho.c ├── params.makefile ├── performance.md ├── points.c ├── polygon_mode.c ├── ray-tracing.md ├── shade_model.c ├── sky.jpg ├── software-implementations.md ├── texture.c ├── texture_fps.cpp ├── texture_transparent.c ├── translate.c ├── transparent.c ├── triangle.c ├── triangle_rotate.c ├── utility-libraries.md ├── vec3.h ├── viewport.c ├── vulkan.md └── wood.jpg ├── plplot ├── Makefile ├── Makefile_params ├── README.md ├── configure └── main.c ├── png ├── .gitignore ├── Makefile ├── Makefile_params ├── README.md └── open_manipulate_write.c ├── posix ├── Makefile ├── README.md ├── access.c ├── bibliography.md ├── common.h ├── configure ├── curses │ ├── Makefile │ ├── Makefile_params │ ├── README.md │ ├── check_key_pressed.c │ ├── clear.c │ ├── common.h │ ├── common_curses.h │ ├── hello.c │ ├── ncurses.md │ ├── printw_newline.c │ ├── printw_newline_screen_height.c │ ├── printw_twice.c │ └── tcgetattr_dump.c ├── execl.c ├── getcwd.c ├── gethostbyname.c ├── gethostname.c ├── getlogin.c ├── getrusage.c ├── getservbyport.c ├── interactive │ ├── .gitignore │ ├── Makefile │ ├── alarm.c │ ├── benchmark │ │ ├── Makefile │ │ ├── README.md │ │ ├── main.c │ │ └── optimize.c │ ├── common.h │ ├── getline.c │ ├── getrusage_sleep.c │ ├── poll-loop.sh │ ├── poll.c │ ├── poll.sh │ ├── pthread_array_sum.c │ ├── pthread_array_sum.gnuplot │ ├── pthread_array_sum.sh │ ├── pthread_cpu_heavy_work.c │ ├── pthread_cpu_heavy_work.gnuplot │ ├── pthread_cpu_heavy_work.sh │ ├── setpgid.c │ ├── setpgid.md │ ├── signal_fork.c │ ├── signal_sleep.c │ └── sleep_1.c ├── ls.c ├── main.c ├── map_private.c ├── mkdir.c ├── netdb.md ├── openat.c ├── pipe.c ├── pthread.md ├── regex.c ├── semaphore.md ├── semaphore_sysv.c ├── setenv.c ├── shm_open.c ├── signal.c ├── signal_open_memstream.c ├── signal_write_safe.c ├── socket │ ├── Makefile │ ├── README.md │ ├── inet │ │ ├── Makefile │ │ ├── Makefile_params │ │ ├── README.md │ │ ├── TODO.md │ │ ├── common.h │ │ ├── receive_file_server.c │ │ ├── rot1_server.c │ │ ├── send_file_client.c │ │ └── send_lines_client.c │ ├── introduction.md │ ├── test │ └── unix │ │ ├── Makefile │ │ ├── README.md │ │ ├── client.c │ │ └── server.c ├── stat.c ├── strdup.c ├── syslog.c ├── uname.c └── write_read.c ├── qt ├── Makefile ├── README.md ├── SlotTest.h ├── configure ├── ico.svg ├── main.cpp ├── qt.pro └── qt_designer.ui ├── scons ├── .gitignore ├── README.md ├── add_option │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── test ├── alias │ ├── README.md │ ├── SConstruct │ └── main.c ├── alias2 │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── main2.c ├── arguments │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── test ├── ccflags │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── test ├── clean ├── command │ ├── .gitignore │ ├── README.md │ ├── SConstruct │ ├── lib.h │ ├── main.c │ └── test ├── default │ ├── README.md │ ├── src0 │ │ ├── SConstruct │ │ └── main.c │ └── src1 │ │ └── main.c ├── define │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── test ├── glob │ ├── README.md │ ├── SConstruct │ ├── lib.c │ ├── lib.h │ ├── main.c │ └── test ├── hello │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── test ├── help │ ├── README.md │ ├── SConstruct │ └── test ├── install │ ├── README.md │ ├── SConstruct │ └── main.c ├── library │ ├── README.md │ ├── SConstruct │ ├── a.c │ ├── a.h │ ├── main.c │ └── test ├── multi_source │ ├── README.md │ ├── SConstruct │ ├── lib.c │ ├── lib.h │ ├── main.c │ └── test ├── multi_target │ ├── README.md │ ├── SConstruct │ ├── main.c │ ├── main2.c │ └── test ├── object │ ├── README.md │ ├── SConstruct │ ├── lib.c │ ├── lib.h │ ├── main.c │ └── test ├── sconscript │ ├── README.md │ ├── SConstruct │ ├── src │ │ ├── SConscript │ │ └── main.c │ └── test ├── sconscript_multi_source │ ├── README.md │ ├── SConstruct │ ├── lib │ │ ├── SConscript │ │ ├── lib.c │ │ └── lib.h │ ├── main.c │ └── test ├── sconscript_variant │ ├── .gitignore │ ├── README.md │ ├── SConstruct │ ├── lib │ │ ├── SConscript │ │ ├── lib.c │ │ └── lib.h │ ├── lib2 │ │ ├── SConscript │ │ ├── lib.c │ │ └── lib.h │ ├── main.c │ └── test ├── test ├── two_envs │ ├── README.md │ ├── SConstruct │ ├── src │ │ └── main.c │ └── test ├── variables │ ├── README.md │ ├── SConstruct │ ├── main.c │ └── test └── variant_dir │ ├── README.md │ ├── SConstruct │ ├── build_0 │ └── main.c │ ├── build_1 │ └── main.c │ ├── src │ └── main.c │ └── test ├── sdl ├── FreeSans.ttf ├── Makefile ├── Makefile_params ├── README.md ├── TODO.md ├── animation.c ├── animation_random_walk.c ├── animation_user_control.c ├── animation_user_control_discrete.c ├── animation_user_control_inertia.c ├── bibliography.md ├── build.md ├── check_a_pressed.c ├── common.h ├── configure ├── error_handling.c ├── flower.png ├── heatmap.c ├── heatmap1d.c ├── heatmap_shader.c ├── heatmap_texture.c ├── introduction.md ├── mouse.c ├── mouse_point_rect.c ├── mouse_rect.c ├── opengl.c ├── png.c ├── render_draw_point.c ├── render_draw_point_rect.c ├── render_draw_rect.c ├── sound_frequency.c ├── texture_and_opengl.c ├── ttf.c ├── two_player_discrete.c ├── two_player_discrete_sync.c └── users.md ├── shared_library ├── README.md ├── abi │ ├── README.md │ ├── clean │ ├── main.c │ ├── test │ ├── v1 │ │ ├── mylib.c │ │ └── mylib.h │ ├── v2_bad │ │ ├── mylib.c │ │ └── mylib.h │ └── v2_good │ │ ├── mylib.c │ │ └── mylib.h ├── basic │ ├── Makefile │ ├── README.md │ ├── a.c │ ├── a.h │ ├── b.c │ ├── b.h │ ├── dlopen.c │ └── main.c ├── lib_lib_dependency │ ├── Makefile │ ├── README.md │ ├── a.c │ ├── a.h │ ├── b.c │ ├── b.h │ └── main.c └── symbol_version │ ├── Makefile │ ├── README.md │ ├── a.c │ ├── a.h │ ├── a.map │ └── main.c ├── swig ├── .gitignore ├── Main.java ├── Makefile ├── README.md ├── configure ├── example.c ├── example.h ├── example.i ├── example.md ├── introduction.md ├── main.py └── setup.py ├── template ├── Makefile ├── Makefile_params ├── README.md ├── configure └── main.c ├── test ├── v4l2 ├── .gitignore ├── Makefile ├── Makefile_params ├── README.md ├── bibliography.md ├── clean ├── common_v4l2.h ├── configure ├── introduction.md ├── take_pictures_contrib.c ├── take_pictures_docs.c ├── take_pictures_docs_min.c ├── take_pictures_opencv_jayrambhia.c.off ├── v4l-utils.md ├── window_show_opengl.c └── window_show_opengl_min.c └── x11 ├── Makefile ├── Makefile_params ├── README.md ├── bibliography.md └── hello_world.c /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | 3 | install: | 4 | cd "$TRAVIS_BUILD_DIR" 5 | ./configure -t 6 | 7 | script: | 8 | cd "$TRAVIS_BUILD_DIR" 9 | ./test 10 | -------------------------------------------------------------------------------- /boost/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /boost/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lm -lboost_graph -lboost_filesystem -lboost_system 2 | -------------------------------------------------------------------------------- /boost/common.hpp: -------------------------------------------------------------------------------- 1 | #include "../cpp/common.hpp" 2 | 3 | #include 4 | #include 5 | 6 | namespace bg = boost::geometry; 7 | namespace bgi = bg::index; 8 | -------------------------------------------------------------------------------- /boost/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libboost-all-dev 4 | -------------------------------------------------------------------------------- /boost/dijkstra.dot: -------------------------------------------------------------------------------- 1 | digraph D { 2 | rankdir=LR 3 | size="4,3" 4 | ratio="fill" 5 | edge[style="bold"] 6 | node[shape="circle"] 7 | 0 -> 2[label="1", color="black"]1 -> 1[label="2", color="grey"]1 -> 3[label="1", color="grey"]1 -> 4[label="2", color="grey"]2 -> 1[label="7", color="grey"]2 -> 3[label="3", color="black"]3 -> 4[label="1", color="black"]4 -> 0[label="1", color="grey"]4 -> 1[label="1", color="black"]} -------------------------------------------------------------------------------- /boost/geometry.md: -------------------------------------------------------------------------------- 1 | # Geometry 2 | 3 | Most important thing is `rtree`, which is like an N-dimensional binary tree, and allows for log(n) queries. 4 | 5 | Also clone the library and look into the ton of examples that come with the source: 6 | 7 | git clone https://github.com/boostorg/geometry 8 | -------------------------------------------------------------------------------- /bullet/.gitignore: -------------------------------------------------------------------------------- 1 | *.dat 2 | *.png 3 | -------------------------------------------------------------------------------- /bullet/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /bullet/Makefile_targets: -------------------------------------------------------------------------------- 1 | DATA_EXT := .dat 2 | IMG_EXT := .png 3 | 4 | DATAS := $(addsuffix $(DATA_EXT), $(OUTS_NOEXT)) 5 | IMGS := $(addsuffix $(IMG_EXT), $(OUTS_NOEXT)) 6 | 7 | PHONY := dat imgs 8 | 9 | data: $(DATAS) 10 | 11 | imgs: $(IMGS) 12 | 13 | $(IN_DIR)%$(DATA_EXT): $(OUT_DIR)%$(OUT_EXT) 14 | ./'$<' >'$@' 15 | 16 | $(IN_DIR)%$(IMG_EXT): $(OUT_DIR)%$(DATA_EXT) 17 | ./plot '$<' 18 | -------------------------------------------------------------------------------- /bullet/benchmarks.md: -------------------------------------------------------------------------------- 1 | # Benchmarks 2 | 3 | 4 | -------------------------------------------------------------------------------- /bullet/bibliography.md: -------------------------------------------------------------------------------- 1 | # Bibliography 2 | 3 | Forums: 4 | 5 | - , gets more replies than Stack Overflow... lol newbs. 6 | - 7 | 8 | Demos without source code: 9 | 10 | - Moby Motion - KEVA Planks - Bullet Physics (HD) , tower destruction contraptions. 11 | - yonarw - (my) openGL engine + Bullet physics - , dynamic shadows 12 | -------------------------------------------------------------------------------- /bullet/clean: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | rm -f *.dat *.png *.pyc 3 | -------------------------------------------------------------------------------- /bullet/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libbullet-dev 4 | sudo pip install -r requirements.txt 5 | -------------------------------------------------------------------------------- /bullet/gravity_collision.py: -------------------------------------------------------------------------------- 1 | import numpy 2 | def plot(plt, data_path): 3 | a = numpy.loadtxt(data_path, skiprows=1, usecols=(0, 4)) 4 | plt.plot( 5 | a[:, 0], 6 | a[:, 1], 7 | 'x', 8 | label='collision' 9 | ) 10 | -------------------------------------------------------------------------------- /bullet/requirements.txt: -------------------------------------------------------------------------------- 1 | matplotlib==1.5.1 2 | numpy==1.11.1 3 | -------------------------------------------------------------------------------- /bullet/source-code.md: -------------------------------------------------------------------------------- 1 | # Source code 2 | 3 | ## Style 4 | 5 | Bullet is ugly :-) 6 | 7 | - bt hard-coded prefix instead of C++ namespace 8 | - tabs, including in the middle of lines 9 | -------------------------------------------------------------------------------- /bullet/source-tree.md: -------------------------------------------------------------------------------- 1 | # Source tree 2 | 3 | Code style is horrible. Whitespaces everywhere. Game programmers ;-) 4 | 5 | - examples 6 | - : tree of examples that appears in the browser 7 | - : tree of examples that appears in the browser 8 | -------------------------------------------------------------------------------- /bullet/users.md: -------------------------------------------------------------------------------- 1 | # Users 2 | 3 | Notables who uses Bullet. 4 | 5 | - Blender 6 | - libgdx: 7 | 8 | Lists: 9 | 10 | - http://bulletphysics.org/Bullet/phpBB3/viewforum.php?f=17 11 | -------------------------------------------------------------------------------- /c/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /c/Makefile_params: -------------------------------------------------------------------------------- 1 | CFLAGS_EXTRA := \ 2 | -DIMPLEMENTATION_SIGNAL \ 3 | -trigraphs \ 4 | -Wno-comment \ 5 | -Wno-ignored-qualifiers \ 6 | -Wno-missing-field-initializers \ 7 | -Wno-overflow \ 8 | -Wno-override-init \ 9 | -Wno-sequence-point \ 10 | -Wno-sign-compare \ 11 | -Wno-uninitialized \ 12 | -Wno-unused-but-set-variable \ 13 | -Wno-unused-label \ 14 | -Wno-unused-local-typedefs \ 15 | -Wno-unused-value \ 16 | -Wno-unused-variable 17 | #-DUNDEFINED_BEHAVIOUR \ 18 | -------------------------------------------------------------------------------- /c/assert_h.c: -------------------------------------------------------------------------------- 1 | /* 2 | # assert.h 3 | 4 | Defines the assert *macro*, which upon failure: 5 | 6 | - prints the file and line of the failure 7 | - calls abort 8 | 9 | Do not forget that `assert` is a *macro*, or this may lead to unexpected effects, such as: 10 | 11 | - unused variable warning because asserts got removed 12 | - in C++, no `std::` namespace 13 | */ 14 | 15 | #include "common.h" 16 | 17 | int main(void) { 18 | assert(1); 19 | return EXIT_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /c/atexit.c: -------------------------------------------------------------------------------- 1 | /* 2 | # atexit 3 | 4 | Function gets called when process ends via `exit` or a `return` on the `main` function. 5 | 6 | Does not get called on abnormal termination, e.g. from signals. 7 | */ 8 | 9 | #include "common.h" 10 | 11 | void atexit_func() { 12 | printf("atexit\n"); 13 | } 14 | 15 | int main(void) { 16 | atexit(atexit_func); 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /c/auto.c: -------------------------------------------------------------------------------- 1 | /* 2 | # auto 3 | 4 | Is the name for the default way to declare var 5 | that is neither volatile nor register. 6 | 7 | Sadly, in C++11 is is something completely different: type inference. 8 | */ 9 | 10 | #include "common.h" 11 | 12 | int main(void) { 13 | /* SAME: */ 14 | int i; 15 | auto int i2; 16 | 17 | /* ERROR: */ 18 | /*auto i3;*/ 19 | 20 | return EXIT_SUCCESS; 21 | } 22 | -------------------------------------------------------------------------------- /c/basic_type.md: -------------------------------------------------------------------------------- 1 | # Basic types 2 | 3 | Types like `int`, `char`, `float` and `_Complex`. 4 | 5 | ## Derived types 6 | 7 | Types which are not based, bur defined by users: arrays, structs and unions. 8 | -------------------------------------------------------------------------------- /c/comments.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Comments 3 | 4 | Get removed by the preprocessor. 5 | */ 6 | 7 | #include "common.h" 8 | 9 | int main(void) { 10 | /* Standard multi line comment. */ 11 | /* 12 | assert(false); 13 | */ 14 | 15 | /* A comment may contain `/ *`, but GCC warns with `-Wcomment`. */ 16 | 17 | #if __STDC_VERSION__ >= 199901L 18 | /* 19 | Double slash comment like in C++ were only introduced in C99. 20 | 21 | Be portable and don't ever use them. 22 | 23 | They are however a part of C++. 24 | */ 25 | //assert(false); 26 | #endif 27 | return EXIT_SUCCESS; 28 | } 29 | -------------------------------------------------------------------------------- /c/exit.c: -------------------------------------------------------------------------------- 1 | /* 2 | # exit 3 | 4 | Exit program at any point, including outside of the main function. 5 | 6 | Gets return value as an argument. 7 | 8 | On Linux, implemented with the sys_exit system call. 9 | */ 10 | 11 | #include "common.h" 12 | 13 | void exit_func() { 14 | exit(EXIT_SUCCESS); 15 | } 16 | 17 | int main(void) { 18 | exit_func(); 19 | /* Never gets run */ 20 | return EXIT_FAILURE; 21 | } 22 | -------------------------------------------------------------------------------- /c/extern/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /c/extern/README.md: -------------------------------------------------------------------------------- 1 | # static 2 | 3 | Multi file semantics of `extern`. 4 | -------------------------------------------------------------------------------- /c/extern/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int commonInt = 1; 4 | int externInt = 1; 5 | 6 | void printA() { 7 | commonInt++; 8 | externInt++; 9 | printf("commonInt = %d\n", commonInt); 10 | printf("externInt = %d\n", externInt); 11 | } 12 | -------------------------------------------------------------------------------- /c/getenv.c: -------------------------------------------------------------------------------- 1 | /* 2 | # getenv 3 | 4 | # Envoronment variables 5 | 6 | Returns NULL if not found. 7 | */ 8 | 9 | #include "common.h" 10 | 11 | int main(void) { 12 | printf("HOME = %s\n", getenv("HOME")); 13 | assert(getenv("NOT_DEFINED") == NULL); 14 | return EXIT_SUCCESS; 15 | } 16 | -------------------------------------------------------------------------------- /c/hello_world.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | puts("hello world"); 6 | return EXIT_SUCCESS; 7 | } 8 | -------------------------------------------------------------------------------- /c/identifier_list.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Identifier list function declarator 3 | 4 | Old style thing that should never be done today. 5 | */ 6 | 7 | #include "common.h" 8 | 9 | /* TODO without definition. Should never be done. Conforming or not? */ 10 | /*int f(x, y);*/ 11 | 12 | int f(x, y) 13 | int x; 14 | int y; 15 | { return x + y; } 16 | 17 | /* Also identifier list: it is the only optional one. */ 18 | void g() {} 19 | 20 | /* Identifier type list. This one is not optional. */ 21 | void h(void) {} 22 | 23 | int main(void) { 24 | assert(f(1, 2) == 3); 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /c/interactive/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /c/interactive/README.md: -------------------------------------------------------------------------------- 1 | # Interactive 2 | 3 | Programs in this directory should be run manually one by one because they do things like: 4 | 5 | - waiting a perceptible amount of time 6 | - asking for user input 7 | - exiting with status different than 1 8 | -------------------------------------------------------------------------------- /c/interactive/branch_prediction_and_patterns_perf.c: -------------------------------------------------------------------------------- 1 | /* 2 | Measure the performance of each call with: TODO 3 | 4 | http://igoro.com/archive/fast-and-slow-if-statements-branch-prediction-in-modern-processors/ 5 | */ 6 | 7 | #include "common.h" 8 | 9 | const long N = 1000000000; 10 | 11 | void f(long p) { 12 | long sum = 0; 13 | for (long i = 0; i < N; i++) 14 | if (!(i & p)) 15 | sum++; 16 | } 17 | 18 | int main(void) { 19 | f(INT_MIN); 20 | f(-1); 21 | f(1); 22 | f(2); 23 | f(3); 24 | f(4); 25 | f(8); 26 | f(16); 27 | return EXIT_SUCCESS; 28 | } 29 | -------------------------------------------------------------------------------- /c/interactive/common.h: -------------------------------------------------------------------------------- 1 | ../common.h -------------------------------------------------------------------------------- /c/iso646_h.c: -------------------------------------------------------------------------------- 1 | /* 2 | # iso646.h 3 | 4 | Obscure header with macros that avoid using characters such as `|` or '~' 5 | which may be hard to type on certain international keyboards. 6 | 7 | - and: && 8 | - and_eq: &= 9 | - bitand: && 10 | - bitor: & 11 | - compl: | 12 | - not: ! 13 | - not_eq: != 14 | - or: || 15 | - or_eq: |= 16 | - xor: ^ 17 | - xor_eq: ^= 18 | */ 19 | 20 | #include "common.h" 21 | 22 | int main(void) { 23 | assert(1 and 1); 24 | assert(0 or 1); 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /c/lexer.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Lexer 3 | */ 4 | 5 | #include "common.h" 6 | 7 | int main(void) { 8 | /* 9 | # Maximal munch rule. 10 | 11 | http://stackoverflow.com/questions/5341202/why-doesnt-ab-work-in-c/24947922#24947922 12 | */ 13 | { 14 | int i = 0; 15 | assert(i+++1 == 1); 16 | assert(i == 1); 17 | } 18 | 19 | return EXIT_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /c/libraries.md: -------------------------------------------------------------------------------- 1 | # Libraries 2 | 3 | Interesting C libraries. 4 | 5 | ## Utilities 6 | 7 | Basic utilities that C does not have. 8 | 9 | 10 | 11 | - , GNOME, data structures 12 | - 13 | - 14 | -------------------------------------------------------------------------------- /c/min.c: -------------------------------------------------------------------------------- 1 | int main(void) { 2 | return 0; 3 | } 4 | -------------------------------------------------------------------------------- /c/ndebug.c: -------------------------------------------------------------------------------- 1 | /* 2 | # NDEBUG 3 | 4 | If defined *before assert.h* is included, asserts do nothing. 5 | 6 | Commonly defined form the command line. 7 | */ 8 | 9 | #define NDEBUG 10 | #include 11 | #include 12 | 13 | int main(void) { 14 | assert(0); 15 | return EXIT_SUCCESS; 16 | } 17 | -------------------------------------------------------------------------------- /c/no_return_non_void.c: -------------------------------------------------------------------------------- 1 | /* 2 | # No return non void 3 | 4 | In C99, a non-void return function can have no return. 5 | 6 | This is because it is hard to check if all code paths have a return. 7 | 8 | Gives undefined behaviour. 9 | */ 10 | 11 | #include "common.h" 12 | 13 | #if __STDC_VERSION__ >= 199901L 14 | #ifdef UNDEFINED_BEHAVIOUR 15 | int f() {} 16 | #endif 17 | #endif 18 | 19 | int main(void) { 20 | #if __STDC_VERSION__ >= 199901L 21 | #ifdef UNDEFINED_BEHAVIOUR 22 | f(); 23 | #endif 24 | #endif 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /c/perror.c: -------------------------------------------------------------------------------- 1 | /* # perror 2 | * 3 | * Print a description of errno to stderr with given prefix appended, `NULL` for no prefix. 4 | * 5 | * Sample output on glibc 2.23: 6 | * 7 | * perror test EDOM: Numerical argument out of domain 8 | */ 9 | 10 | #include "common.h" 11 | 12 | int main(void) { 13 | errno = EDOM; 14 | perror("perror test EDOM"); 15 | return EXIT_SUCCESS; 16 | } 17 | -------------------------------------------------------------------------------- /c/pointer_to_int.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Pointer to int typecasts 3 | 4 | Implementation defined. 5 | 6 | http://stackoverflow.com/questions/7677919/unexpected-sign-extension-of-int32-or-32bit-pointer-when-converted-to-uint64 7 | 8 | TODO convert forward and back guaranteed equal? 9 | */ 10 | 11 | #include "common.h" 12 | 13 | int main(void) { 14 | return EXIT_SUCCESS; 15 | } 16 | -------------------------------------------------------------------------------- /c/preprocessor.inc: -------------------------------------------------------------------------------- 1 | i++; 2 | -------------------------------------------------------------------------------- /c/static/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /c/static/README.md: -------------------------------------------------------------------------------- 1 | # static 2 | 3 | Multi file examples of `static`. 4 | 5 | 1. [Variable](variable/) 6 | 1. [Function](function/) 7 | -------------------------------------------------------------------------------- /c/static/function/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /c/static/function/README.md: -------------------------------------------------------------------------------- 1 | http://stackoverflow.com/questions/558122/what-is-a-static-function/30319812#30319812 2 | -------------------------------------------------------------------------------- /c/static/function/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | /* OK: only declared, not defined. Will use the one in notmain. */ 5 | int notmain_add(void); 6 | 7 | /* OK: only visible to this file. */ 8 | static int static_func(void) { 9 | return 0x10; 10 | } 11 | 12 | int main_func(void) { 13 | return 0x1; 14 | } 15 | 16 | int main_add(void) { 17 | return main_func() + static_func(); 18 | } 19 | 20 | int main(void) { 21 | assert(main_add() == 0x011); 22 | assert(notmain_add() == 0x101); 23 | return EXIT_SUCCESS; 24 | } 25 | -------------------------------------------------------------------------------- /c/static/function/notmain.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | /* Link time error: already defined in main. */ 4 | /*int main_func() { puts("si"); }*/ 5 | 6 | /* OK: only declared, not defined. Will use the one in main. */ 7 | int main_func(void); 8 | 9 | /* OK: only visible to this file. */ 10 | static int static_func(void) { 11 | return 0x100; 12 | } 13 | 14 | int notmain_add(void) { 15 | return static_func() + main_func(); 16 | } 17 | -------------------------------------------------------------------------------- /c/static/variable/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /c/static/variable/README.md: -------------------------------------------------------------------------------- 1 | http://stackoverflow.com/questions/572547/what-does-static-mean-in-a-c-program/14339047#14339047 2 | -------------------------------------------------------------------------------- /c/static/variable/notmain.h: -------------------------------------------------------------------------------- 1 | #ifndef A_H 2 | #define A_H 3 | 4 | void notmain_func( 5 | int *i_out, 6 | int *static_i_out, 7 | int *static_i_function_out 8 | ); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /c/stdbool_h.c: -------------------------------------------------------------------------------- 1 | /* 2 | # stdbool.h 3 | 4 | # bool 5 | 6 | Macro to `_Bool`. 7 | 8 | Same rationale as `_Complex` vs `complex`. 9 | 10 | # true 11 | 12 | Macro to `1`. 13 | 14 | # false 15 | 16 | Macro to `0`. 17 | */ 18 | 19 | #include "common.h" 20 | 21 | int main(void) { 22 | #if __STDC_VERSION__ >= 199901L 23 | bool b = true; 24 | bool b2 = false; 25 | assert(false == 0); 26 | assert(true == 1); 27 | #endif 28 | return EXIT_SUCCESS; 29 | } 30 | -------------------------------------------------------------------------------- /c/strftime.c: -------------------------------------------------------------------------------- 1 | /* Convert time to a formatted string. 2 | * https://stackoverflow.com/questions/1442116/how-to-get-the-date-and-time-values-in-a-c-program/30759067#30759067 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | int main(void) { 10 | time_t t = time(NULL); 11 | struct tm *tm = localtime(&t); 12 | char s[64]; 13 | size_t ret = strftime(s, sizeof(s), "%c", tm); 14 | assert(ret); 15 | printf("%s\n", s); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /c/template.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | int main(void) { 4 | return EXIT_SUCCESS; 5 | } 6 | -------------------------------------------------------------------------------- /c/template_large.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | #if __STDC_VERSION__ >= 199901L 4 | #endif 5 | 6 | #if __STDC_VERSION__ >= 201112L 7 | #endif 8 | 9 | #ifdef UNDEFINED_BEHAVIOUR 10 | #endif 11 | 12 | int main(void) { 13 | return EXIT_SUCCESS; 14 | } 15 | -------------------------------------------------------------------------------- /c/trivia.md: -------------------------------------------------------------------------------- 1 | # Trivia 2 | 3 | C is called C because it came after B. B is called B because of Bell Labs. 4 | 5 | ## IOCCC 6 | 7 | 8 | 9 | . Amazing. 10 | 11 | IOCCC entry explained in detail: 12 | -------------------------------------------------------------------------------- /c/type_qualifier.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Type qualifier 3 | 4 | Can be added to types to modify their semantics. 5 | */ 6 | 7 | #include "common.h" 8 | 9 | int main(void) { 10 | /* Repeating qualifiers is OK and has no effect. C99 9.7.3.4 */ 11 | { 12 | const const const const int i = 0; 13 | 14 | /* In practice, this is more likely to happen when the qualifier is hidden on a typedef. */ 15 | { 16 | typedef const int ci; 17 | const ci i; 18 | } 19 | } 20 | 21 | return EXIT_SUCCESS; 22 | } 23 | -------------------------------------------------------------------------------- /c/void_p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/c/void_p -------------------------------------------------------------------------------- /c/void_parameter.c: -------------------------------------------------------------------------------- 1 | /* 2 | # void parameter 3 | 4 | http://stackoverflow.com/questions/693788/c-void-arguments 5 | */ 6 | 7 | #include "common.h" 8 | 9 | /* Prototype that takes no arguments. */ 10 | void void_arg(void); 11 | 12 | /* ERROR: void must be the only parameter */ 13 | /*void void_int_arg(int, void);*/ 14 | 15 | /* WARN: parameter has void type */ 16 | /*void void_arg2(void v);*/ 17 | 18 | /* ERROR: incomplete type. */ 19 | /*void void_arg2(void v) {}*/ 20 | 21 | void def(void) {} 22 | 23 | int main(void) { 24 | return EXIT_SUCCESS; 25 | } 26 | -------------------------------------------------------------------------------- /c_from_cpp/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | .PHONY: clean run test 4 | 5 | main.out: main.o notmain.o 6 | g++ -o '$@' $^ 7 | 8 | main.o: main.cpp 9 | g++ -c -g -o '$@' -std=c++98 -Wall -Wextra -pedantic-errors '$<' 10 | 11 | notmain.o: notmain.c 12 | gcc -c -g -o '$@' -std=c89 -Wall -Wextra -pedantic-errors '$<' 13 | 14 | clean: 15 | rm -f *.o *.out 16 | 17 | test: main.out 18 | ./main.out 19 | -------------------------------------------------------------------------------- /c_from_cpp/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c/30526795#30526795 2 | -------------------------------------------------------------------------------- /c_from_cpp/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "notmain.h" 4 | 5 | int main() { 6 | assert(notmain_func() == 1); 7 | } 8 | -------------------------------------------------------------------------------- /c_from_cpp/notmain.c: -------------------------------------------------------------------------------- 1 | #include "notmain.h" 2 | 3 | int notmain_func(void) { return 1; } 4 | -------------------------------------------------------------------------------- /c_from_cpp/notmain.h: -------------------------------------------------------------------------------- 1 | #ifndef NOTMAIN_H 2 | #define NOTMAIN_H 3 | 4 | /* This ifdef allows the header to be used from both C and C++. */ 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | int notmain_func(void); 9 | #ifdef __cplusplus 10 | } 11 | #endif 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /cmake/hello/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | project(ProjectName) 3 | add_executable(main main.c) 4 | -------------------------------------------------------------------------------- /cmake/hello/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | puts(__FILE__); 6 | return EXIT_SUCCESS; 7 | } 8 | -------------------------------------------------------------------------------- /cmake/multi_executable/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | file(GLOB SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "*.c") 3 | foreach(testsourcefile ${SOURCES}) 4 | string(REPLACE ".c" "" testname ${testsourcefile}) 5 | add_executable(${testname} ${testsourcefile}) 6 | endforeach(testsourcefile ${SOURCES}) 7 | -------------------------------------------------------------------------------- /cmake/multi_executable/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | puts(__FILE__); 6 | return EXIT_SUCCESS; 7 | } 8 | -------------------------------------------------------------------------------- /cmake/multi_executable/b.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | puts(__FILE__); 6 | return EXIT_SUCCESS; 7 | } 8 | -------------------------------------------------------------------------------- /cmake/multi_file/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(ProjectName) 2 | cmake_minimum_required(VERSION 3.0) 3 | file(GLOB SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "*.c") 4 | add_executable(main ${SOURCES}) 5 | -------------------------------------------------------------------------------- /cmake/multi_file/a.c: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | 3 | int a(void) { return 42; } 4 | -------------------------------------------------------------------------------- /cmake/multi_file/a.h: -------------------------------------------------------------------------------- 1 | #ifndef A_H 2 | #define A_H 3 | int a(void); 4 | #endif 5 | -------------------------------------------------------------------------------- /cmake/multi_file/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "a.h" 5 | 6 | int main(void) { 7 | printf("%d\n", a()); 8 | return EXIT_SUCCESS; 9 | } 10 | -------------------------------------------------------------------------------- /cmake/multi_file_recursive/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} "src/*.c") 3 | add_executable(main ${SOURCES}) 4 | -------------------------------------------------------------------------------- /cmake/multi_file_recursive/src/d/a.c: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | 3 | int a(void) { return 42; } 4 | -------------------------------------------------------------------------------- /cmake/multi_file_recursive/src/d/a.h: -------------------------------------------------------------------------------- 1 | #ifndef A_H 2 | #define A_H 3 | int a(void); 4 | #endif 5 | -------------------------------------------------------------------------------- /cmake/multi_file_recursive/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "d/a.h" 5 | 6 | int main(void) { 7 | printf("%d\n", a()); 8 | return EXIT_SUCCESS; 9 | } 10 | -------------------------------------------------------------------------------- /cmake/shared_lib_external/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # sudo apt-get install libgsl-dev 2 | cmake_minimum_required(VERSION 2.8) 3 | add_executable(main main.c) 4 | target_link_libraries(main gsl gslcblas) 5 | -------------------------------------------------------------------------------- /cmake/shared_lib_external/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | int main(void) { 7 | double x = 5.0; 8 | printf("J0(%g) = %.18e\n", x, gsl_sf_bessel_J0(x)); 9 | return EXIT_SUCCESS; 10 | } 11 | -------------------------------------------------------------------------------- /cpp/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /cpp/Makefile_params: -------------------------------------------------------------------------------- 1 | CXXFLAGS_EXTRA := -Wno-sequence-point -Wno-unused-variable -Wno-unused-but-set-variable 2 | -------------------------------------------------------------------------------- /cpp/TODO.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | 3 | - call BaseAbstract constructor possible from initializer list and only at that point 4 | - chrono 5 | 6 | using high_resolution_clock = system_clock; 7 | -------------------------------------------------------------------------------- /cpp/bitset.cpp: -------------------------------------------------------------------------------- 1 | #include "common.hpp" 2 | 3 | int main() { 4 | // String init. 5 | { 6 | // Basic. 7 | { 8 | std::bitset<4> s("1010"); 9 | std::cout << s << std::endl; 10 | assert(!s[0]); 11 | } 12 | 13 | // Missing bits: MSB filled with zeroes. 14 | { 15 | std::bitset<4> s("10"); 16 | assert(s == std::bitset<4>("0010")); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /cpp/bool.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | # bool 3 | 4 | in C++, unlike in C, bool is part of the language. 5 | */ 6 | 7 | #include "common.hpp" 8 | 9 | int main() { 10 | bool b; 11 | 12 | b = true; 13 | b = 1; 14 | 15 | if (true) 16 | { 17 | assert(true); 18 | } 19 | if (false) 20 | { 21 | assert(false); 22 | } 23 | 24 | { 25 | std::stringstream oss; 26 | oss << true; 27 | assert(oss.str() == "1"); 28 | } 29 | 30 | { 31 | std::stringstream oss; 32 | oss << false; 33 | assert(oss.str() == "0"); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /cpp/deque.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | # deque 3 | 4 | Double ended queue. 5 | 6 | Random access. 7 | 8 | Very similar interface to std::vector, except that: 9 | 10 | - insertion to front is O(1) 11 | - there is no guarantee of inner storage contiguity 12 | 13 | Discussion on when to use deque or std::vector: 14 | http://stackoverflow.com/questions/5345152/why-would-i-prefer-using-vector-to-deque 15 | 16 | It is controversial if one should use deque or std::vector as the main generic container. 17 | */ 18 | 19 | #include "common.hpp" 20 | 21 | int main() { 22 | // TODO example 23 | } 24 | -------------------------------------------------------------------------------- /cpp/extern_const/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /cpp/extern_const/Makefile_params: -------------------------------------------------------------------------------- 1 | CXX_STD = c++11 2 | -------------------------------------------------------------------------------- /cpp/extern_const/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c/53883715#53883715 2 | -------------------------------------------------------------------------------- /cpp/extern_const/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "notmain.hpp" 4 | 5 | int main() { 6 | // Both files see the same memory address. 7 | assert(¬main_i == notmain_func()); 8 | assert(notmain_i == 42); 9 | } 10 | -------------------------------------------------------------------------------- /cpp/extern_const/notmain.cpp: -------------------------------------------------------------------------------- 1 | #include "notmain.hpp" 2 | 3 | const int notmain_i = 42; 4 | 5 | const int* notmain_func() { 6 | return ¬main_i; 7 | } 8 | -------------------------------------------------------------------------------- /cpp/extern_const/notmain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NOTMAIN_HPP 2 | #define NOTMAIN_HPP 3 | 4 | extern const int notmain_i; 5 | 6 | const int* notmain_func(); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /cpp/goto_cross_initialization.cpp: -------------------------------------------------------------------------------- 1 | // http://stackoverflow.com/questions/5685471/error-jump-to-case-label 2 | 3 | int main() { 4 | goto after; 5 | #if 0 6 | // error: jump to label ‘after’ 7 | // note: crosses initialization of ‘int i’ 8 | int i = 0; 9 | #endif 10 | after: 11 | ; 12 | } 13 | -------------------------------------------------------------------------------- /cpp/hello_world.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "hello world" << std::endl; 5 | } 6 | -------------------------------------------------------------------------------- /cpp/inline_variable/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /cpp/inline_variable/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/177437/what-does-const-static-mean-in-c-and-c/53883715#53883715 2 | -------------------------------------------------------------------------------- /cpp/inline_variable/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "notmain.hpp" 4 | 5 | int main() { 6 | // Both files see the same memory address. 7 | assert(¬main_i == notmain_func()); 8 | assert(notmain_i == 42); 9 | } 10 | -------------------------------------------------------------------------------- /cpp/inline_variable/notmain.cpp: -------------------------------------------------------------------------------- 1 | #include "notmain.hpp" 2 | 3 | const int* notmain_func() { 4 | return ¬main_i; 5 | } 6 | -------------------------------------------------------------------------------- /cpp/inline_variable/notmain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NOTMAIN_HPP 2 | #define NOTMAIN_HPP 3 | 4 | inline constexpr int notmain_i = 42; 5 | 6 | const int* notmain_func(); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /cpp/interactive/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /cpp/interactive/common.hpp: -------------------------------------------------------------------------------- 1 | ../common.hpp -------------------------------------------------------------------------------- /cpp/raw_string_literal.cpp: -------------------------------------------------------------------------------- 1 | // https://stackoverflow.com/questions/1135841/c-multiline-string-literal 2 | 3 | #include "common.hpp" 4 | 5 | int main() { 6 | #if __cplusplus >= 201103L 7 | assert(std::string(R"(a)") == std::string("a")); 8 | assert(std::string(R"(a 9 | b)") == std::string("a\nb")); 10 | assert(std::string(R"( 11 | a)") == std::string("\na")); 12 | assert(std::string(R"asdf()")asdf") == std::string(")\"")); 13 | #endif 14 | } 15 | -------------------------------------------------------------------------------- /cpp/regex.cpp: -------------------------------------------------------------------------------- 1 | // # regex 2 | // 3 | // Finally they are supported! 4 | // 5 | // Many flavours supported: JavaScript, grep, awk, ... 6 | // 7 | // It is probably saner and more powerful to stick to JavaScript regexes. 8 | 9 | #include "common.hpp" 10 | 11 | int main() { 12 | #if __cplusplus >= 201103L 13 | // Js has `[:d:]` equivalent POSIX `[:digit:]`. 14 | std::regex r("a.(c|d)"); 15 | #endif 16 | } 17 | -------------------------------------------------------------------------------- /cpp/rtti.md: -------------------------------------------------------------------------------- 1 | # RTTI 2 | 3 | Run time type information. 4 | 5 | Any function that gets class information explicitly at runtime: 6 | 7 | - `typeid` 8 | - `dynamic_cast` 9 | 10 | Google style 3.26 discourages this, since if you really need it your design is probably flawed. 11 | 12 | Also using `typeid` on variables means that extra meta data must be kept about those variables. 13 | -------------------------------------------------------------------------------- /cpp/static_cast.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | # static_cast 3 | 4 | http://en.cppreference.com/w/cpp/language/static_cast 5 | 6 | - done at compile time 7 | - may results in an extra copy constructor call, and thus extra assembly intructions 8 | - always returns a temporary 9 | - cannot change const and volatile (CV). Use const_cast for that. 10 | */ 11 | 12 | #include "common.hpp" 13 | 14 | int main() { 15 | // TODO. 16 | } 17 | -------------------------------------------------------------------------------- /cpp/style-guides.md: -------------------------------------------------------------------------------- 1 | # Style guides 2 | 3 | - 4 | 5 | Google C++ coding standards. 6 | 7 | Interesting points: 8 | 9 | - a Boost subset is accepted 10 | - a C++11 subset is accepted 11 | 12 | - 13 | 14 | CERT C++ Coding Standard. 15 | 16 | Focus on security. 17 | 18 | By Carnegie Mellon University. 19 | 20 | - 21 | 22 | Coding guidelines, clearly exemplified. 23 | -------------------------------------------------------------------------------- /cpp/template_instantiation/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /cpp/template_instantiation/Makefile_params: -------------------------------------------------------------------------------- 1 | CXX_STD = c++11 2 | -------------------------------------------------------------------------------- /cpp/template_instantiation/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "notmain.hpp" 4 | 5 | int main() { 6 | std::cout << MyClass().f(1) << std::endl; 7 | #if 0 8 | // error: undefined reference to `X::f(float)' 9 | std::cout << X().f(1.5) << std::endl; 10 | #endif 11 | } 12 | -------------------------------------------------------------------------------- /cpp/template_instantiation/notmain.cpp: -------------------------------------------------------------------------------- 1 | #include "notmain.hpp" 2 | 3 | template 4 | T MyClass::f(T t) { return t + 1; } 5 | 6 | // Only int is instantiated. 7 | template class MyClass; 8 | -------------------------------------------------------------------------------- /cpp/template_instantiation/notmain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NOTMAIN_HPP 2 | #define NOTMAIN_HPP 3 | 4 | template struct MyClass { 5 | T f(T t); 6 | }; 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /cpp/template_on_header/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /cpp/template_on_header/Makefile_params: -------------------------------------------------------------------------------- 1 | CXX_STD = c++11 2 | -------------------------------------------------------------------------------- /cpp/template_on_header/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mytemplate.hpp" 4 | #include "notmain.hpp" 5 | 6 | int main() { 7 | std::cout << notmain() + MyTemplate().f(1) << std::endl; 8 | } 9 | -------------------------------------------------------------------------------- /cpp/template_on_header/mytemplate.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MYTEMPLATE_HPP 2 | #define MYTEMPLATE_HPP 3 | 4 | template 5 | struct MyTemplate { 6 | T f(T t) { return t + 1; } 7 | }; 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /cpp/template_on_header/notmain.cpp: -------------------------------------------------------------------------------- 1 | #include "mytemplate.hpp" 2 | #include "notmain.hpp" 3 | 4 | int notmain() { return MyTemplate().f(1); } 5 | -------------------------------------------------------------------------------- /cpp/template_on_header/notmain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NOTMAIN_HPP 2 | #define NOTMAIN_HPP 3 | 4 | int notmain(); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /cpp/trivia.md: -------------------------------------------------------------------------------- 1 | # Trivia 2 | 3 | - 4 | 5 | Obscure language features. 6 | 7 | - 8 | 9 | How can this have so many upvotes? 10 | 11 | - 12 | 13 | Funny... 14 | 15 | ## History 16 | 17 | - 18 | 19 | C++ actually compiled to C in the olden days. 20 | -------------------------------------------------------------------------------- /cpp_from_c/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | .PHONY: run clean 4 | 5 | main.out: main.o notmain.o 6 | g++ -o '$@' $^ 7 | 8 | notmain.o: notmain.cpp 9 | g++ -c -g -o '$@' -pedantic-errors -std=c++98 -Wall -Wextra '$<' 10 | 11 | main.o: main.c 12 | gcc -c -g -o '$@' -pedantic-errors -std=c89 -Wall -Wextra '$<' 13 | 14 | clean: 15 | rm -f *.o *.out 16 | 17 | test: main.out 18 | ./main.out 19 | -------------------------------------------------------------------------------- /cpp_from_c/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c/30526795#30526795 2 | -------------------------------------------------------------------------------- /cpp_from_c/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "notmain.hpp" 4 | 5 | int main(void) { 6 | assert(f_int(1) == 2); 7 | assert(f_float(1.0) == 3); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /cpp_from_c/notmain.cpp: -------------------------------------------------------------------------------- 1 | #include "notmain.hpp" 2 | 3 | int f(int i) { 4 | return i + 1; 5 | } 6 | 7 | int f(float i) { 8 | return i + 2; 9 | } 10 | 11 | int f_int(int i) { 12 | return f(i); 13 | } 14 | 15 | int f_float(float i) { 16 | return f(i); 17 | } 18 | -------------------------------------------------------------------------------- /cpp_from_c/notmain.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NOTMAIN_HPP 2 | #define NOTMAIN_HPP 3 | 4 | #ifdef __cplusplus 5 | // C cannot see these overloaded prototypes, or else it would get confused. 6 | int f(int i); 7 | int f(float i); 8 | extern "C" { 9 | #endif 10 | int f_int(int i); 11 | int f_float(float i); 12 | #ifdef __cplusplus 13 | } 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /cuda/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | .PHONY: all 4 | 5 | CCC ?= nvcc 6 | IN_EXT ?= .cu 7 | OUT_EXT ?= .out 8 | 9 | OUTS := $(addsuffix $(OUT_EXT), $(basename $(wildcard *$(IN_EXT)))) 10 | 11 | all: $(OUTS) 12 | 13 | %$(OUT_EXT): %$(IN_EXT) 14 | $(CCC) -o '$@' '$<' 15 | 16 | clean: 17 | rm *'$(OUT_EXT)' 18 | -------------------------------------------------------------------------------- /cuda/c_cu/README.md: -------------------------------------------------------------------------------- 1 | # C cu 2 | 3 | Failed attempt to mix CUDA and C/C++ and compile it with GCC. 4 | 5 | 6 | -------------------------------------------------------------------------------- /cuda/c_cu/inc.cu: -------------------------------------------------------------------------------- 1 | /* Minimal example. Increment a vector. */ 2 | 3 | #define N 3 4 | 5 | __global__ void inc(int *a) { 6 | int i = blockIdx.x; 7 | if (i 4 | 5 | #include 6 | 7 | int main() { 8 | int ha[N], *da; 9 | cudaMalloc((void **)&da, N*sizeof(int)); 10 | for (int i = 0; i>>(da); 15 | cudaMemcpy(ha, da, N*sizeof(int), cudaMemcpyDeviceToHost); 16 | for (int i = 0; i < N; ++i) { 17 | assert(ha[i] == i + 1); 18 | } 19 | cudaFree(da); 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /eigen/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /eigen/Makefile_params: -------------------------------------------------------------------------------- 1 | CXXFLAGS_EXTRA := $(shell pkg-config --cflags eigen3) 2 | -------------------------------------------------------------------------------- /eigen/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | # BLAS C/Fotran and LAPACK Fortran: 4 | sudo apt-get install liblapack-dev liblapacke-dev 5 | # Lapack C via LAPACKE: 6 | # TODO: how to install on Ubuntu 12.04? 7 | # The following works only on later Ubuntu 8 | #sudo apt-get install liblapacke-dev 9 | -------------------------------------------------------------------------------- /ffmpeg/.gitignore: -------------------------------------------------------------------------------- 1 | /sample-media/ 2 | /tmp.* 3 | -------------------------------------------------------------------------------- /ffmpeg/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /ffmpeg/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lavdevice -lavfilter -lavformat -lavcodec -lpostproc -lswresample -lswscale -lavutil -lXv -lX11 -lXext -lxcb -lxcb-shm -lxcb -lxcb-xfixes -lxcb-render -lxcb-shape -lxcb -lxcb-shape -lxcb -lX11 -lasound -lx264 -lm -llzma -lz -pthread 2 | -------------------------------------------------------------------------------- /ffmpeg/README.md: -------------------------------------------------------------------------------- 1 | # FFmpeg 2 | 3 | 1. Examples 4 | 1. [encode](encode.c) 5 | 1. [resize](resize.c) 6 | 1. Theory 7 | 1. [Introduction](introduction.md) 8 | 1. [libav](libav.md) 9 | 1. [Source tree](source-tree.md) 10 | 1. [Architecture](architecture.md) 11 | 1. [Build](build.md) 12 | 1. [Trivia](trivia.md) 13 | 1. [Bibliography](bibliography.md) 14 | -------------------------------------------------------------------------------- /ffmpeg/TODO.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | 3 | - server: Write two programs, one that sends a video file / camera input from one computer, the other that receives it and shows to screen or saves it to a file. 4 | -------------------------------------------------------------------------------- /ffmpeg/architecture.md: -------------------------------------------------------------------------------- 1 | # Architecture 2 | 3 | FFmpeg has CPU-specific acceleration optimizations on the source tree, see e.g.: `ls libavcodec`. TODO: how much is this faster? 4 | 5 | TODO: what about GPU? `git grep -i GPU` gives some hits. 6 | -------------------------------------------------------------------------------- /ffmpeg/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install \ 4 | libavutil-dev \ 5 | libx264-dev \ 6 | libvpx-dev 7 | -------------------------------------------------------------------------------- /ffmpeg/libav.md: -------------------------------------------------------------------------------- 1 | # libav 2 | 3 | Fork because of war. Still highly compatible. 4 | 5 | History of the fork: https://en.wikipedia.org/wiki/Libav#History 6 | 7 | libav is going to die now that Debian switched back to FFmpeg in 2015? 8 | 9 | `libav***` are libraries present inside of FFmpeg. 10 | 11 | Each of those libraries can be versioned separately through the `version.h` header. 12 | 13 | -------------------------------------------------------------------------------- /ffmpeg/source-tree.md: -------------------------------------------------------------------------------- 1 | # Source tree 2 | 3 | - `AVPicture` 4 | 5 | - `AVFrame` TODO: what is meant by frame exactly in each codec type? 6 | 7 | Stores each plane separately, as the format is more manageable. 8 | 9 | You can get a raw byte array with: 10 | 11 | avpicture_layout 12 | -------------------------------------------------------------------------------- /ffmpeg/trivia.md: -------------------------------------------------------------------------------- 1 | # Trivia 2 | 3 | File stealing vulnerability January 2016: 4 | 5 | Does YouTube use it or not? 6 | 7 | - 8 | - 9 | -------------------------------------------------------------------------------- /flex_bison/bison/test: -------------------------------------------------------------------------------- 1 | ../test -------------------------------------------------------------------------------- /flex_bison/bison/test_io.py: -------------------------------------------------------------------------------- 1 | inouts = [ 2 | ["print", "print 1;\n", "1\n"], 3 | ["print variable", "a = 1; print a;\n", "1\n"], 4 | ["variables", "a = 1; b = a; print b;", "1\n"], 5 | ["variables", "a = 1 + 1; print a;", "2\n"], 6 | ["if", "if(1); print 1;", "1\n"], 7 | ["while", "i = 0; while (i < 3) {print i; i = i + 1;}", "0\n1\n2\n"], 8 | ] 9 | -------------------------------------------------------------------------------- /flex_bison/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Ubuntu 3 | sudo apt-get update 4 | sudo apt-get install flex bison 5 | -------------------------------------------------------------------------------- /flex_bison/cpp/README.md: -------------------------------------------------------------------------------- 1 | # Flex with C++ 2 | 3 | Example of how to use Flex/Yacc with C++. 4 | 5 | Original source: . 6 | -------------------------------------------------------------------------------- /flex_bison/cpp/flex.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include "heading.h" 3 | #include "bison.h" 4 | int yyerror(char *s); 5 | //int yylineno = 1; 6 | %} 7 | 8 | digit [0-9] 9 | int_const {digit}+ 10 | 11 | %% 12 | 13 | {int_const} { yylval.int_val = atoi(yytext); return INTEGER_LITERAL; } 14 | "+" { yylval.op_val = new std::string(yytext); return PLUS; } 15 | "*" { yylval.op_val = new std::string(yytext); return MULT; } 16 | 17 | [ \t]* {} 18 | [\n] { yylineno++; } 19 | 20 | . { std::cerr << "SCANNER "; yyerror(""); exit(1); } 21 | -------------------------------------------------------------------------------- /flex_bison/cpp/heading.h: -------------------------------------------------------------------------------- 1 | #define YY_NO_UNPUT 2 | 3 | using namespace std; 4 | 5 | #include 6 | #include 7 | #include 8 | -------------------------------------------------------------------------------- /flex_bison/cpp/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "heading.h" 4 | 5 | // prototype of bison-generated parser function 6 | int yyparse(); 7 | 8 | int main(int argc, char **argv) 9 | { 10 | if ( (argc > 1) && (freopen(argv[1], "r", stdin) == NULL) ) 11 | { 12 | cerr << argv[0] << ": File " << argv[1] << " cannot be opened.\n"; 13 | exit( EXIT_FAILURE ); 14 | } 15 | 16 | yyparse(); 17 | 18 | return EXIT_SUCCESS; 19 | } 20 | -------------------------------------------------------------------------------- /flex_bison/cpp/test: -------------------------------------------------------------------------------- 1 | ../test -------------------------------------------------------------------------------- /flex_bison/cpp/test_io.py: -------------------------------------------------------------------------------- 1 | inouts = [ 2 | ["sum", "1+1", "2\n" ], 3 | ["mult", "1*1", "1\n" ], 4 | ["sum mult", "1+1*0", "1\n" ], 5 | ["mult sum", "0*1+1", "1\n" ], 6 | ] 7 | -------------------------------------------------------------------------------- /flex_bison/flex/README.md: -------------------------------------------------------------------------------- 1 | # Flex 2 | 3 | Main cheat on Flex. Demonstrates Flex without Bison. 4 | -------------------------------------------------------------------------------- /flex_bison/flex/test: -------------------------------------------------------------------------------- 1 | ../test -------------------------------------------------------------------------------- /fortran/Makefile: -------------------------------------------------------------------------------- 1 | EXT ?= f 2 | RUN ?= main 3 | 4 | .PROXY: clean run 5 | 6 | main: $(RUN).$(EXT) 7 | gfortran -o '$(RUN)' '$(RUN).$(EXT)' 8 | 9 | clean: 10 | rm '$(RUN)' 11 | 12 | run: $(RUN) 13 | ./'$(RUN)' 14 | -------------------------------------------------------------------------------- /fortran/README.md: -------------------------------------------------------------------------------- 1 | # Fortran 2 | 3 | Fortran cheatsheet: [main.f](main.f) 4 | 5 | Requirement: `gfortran`. 6 | -------------------------------------------------------------------------------- /fortran/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get install gfortran 3 | -------------------------------------------------------------------------------- /fortran_from_c/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: run clean 2 | 3 | main.out: main.o f.o 4 | gcc -o main.out main.o f.o -l:libgfortran.so.3 5 | 6 | run: main.out 7 | ./main.out 8 | 9 | main.o: main.c 10 | gcc -c "$<" -o "$@" 11 | 12 | f.o: f.f 13 | gfortran -c "$<" -o "$@" 14 | 15 | clean: 16 | rm -rf *.o *.out 17 | -------------------------------------------------------------------------------- /fortran_from_c/f.h: -------------------------------------------------------------------------------- 1 | #ifndef F_H 2 | #define F_H 3 | 4 | void s_(int* i, float* r, double* d, int* is2x2); 5 | 6 | int f_(int* i); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /gcc/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /gcc/Makefile_params: -------------------------------------------------------------------------------- 1 | CC_STD = gnu11 2 | PEDANTIC = 3 | -------------------------------------------------------------------------------- /gcc/ada/.gitignore: -------------------------------------------------------------------------------- 1 | *.ali 2 | main 3 | -------------------------------------------------------------------------------- /gcc/ada/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | .PHONY: clean run 3 | 4 | main: main.adb 5 | gcc -c main.adb 6 | gnatbind main 7 | gnatlink main 8 | # Same: 9 | #gnatmake main.adb 10 | 11 | clean: 12 | rm -f main main.ali main.o 13 | 14 | run: main 15 | ./main 16 | -------------------------------------------------------------------------------- /gcc/ada/README.md: -------------------------------------------------------------------------------- 1 | # ADA 2 | 3 | GCC uses `gnat1` as the internal compiler, analogous to `cc1`. 4 | 5 | ADA cannot be linked directly: there is an intermediary `bind` step that does extra consistency checks. 6 | -------------------------------------------------------------------------------- /gcc/ada/main.adb: -------------------------------------------------------------------------------- 1 | with Ada.Text_IO; use Ada.Text_IO; 2 | procedure Main is 3 | begin 4 | Put_Line ("hello world"); 5 | end Main; 6 | -------------------------------------------------------------------------------- /gcc/alloca.c: -------------------------------------------------------------------------------- 1 | /* 2 | https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Variable-Length.html 3 | 4 | Allocate on the stack frame. 5 | 6 | man alloca 7 | 8 | Thus automatically deallocated when the function returns. 9 | 10 | http://stackoverflow.com/questions/1018853/why-is-alloca-not-considered-good-practice 11 | */ 12 | 13 | #include "common.h" 14 | 15 | int f() { 16 | size_t size = 1 + (time(NULL) % 3); 17 | int *ip = (int *)alloca(size * sizeof(int)); 18 | ip[0] = 1; 19 | return ip[0]; 20 | } 21 | 22 | int main(void) { 23 | assert(f() == 1); 24 | return EXIT_SUCCESS; 25 | } 26 | -------------------------------------------------------------------------------- /gcc/array_range_initialization.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Array range initialization 3 | */ 4 | 5 | #include "common.h" 6 | 7 | int main(void) { 8 | int is[] = { 9 | [0 ... 2] = 0, 10 | [3 ... 5 ] = 1 11 | }; 12 | assert(memcmp(is, 13 | &(int[6]){ 0, 0, 0, 1, 1, 1 }, 14 | sizeof(typeof(is))) == 0); 15 | return EXIT_SUCCESS; 16 | } 17 | -------------------------------------------------------------------------------- /gcc/auto_type.c: -------------------------------------------------------------------------------- 1 | /* 2 | ## __auto_type 3 | 4 | Like C++11 `auto`. 5 | */ 6 | 7 | #include "common.h" 8 | 9 | int main(void) { 10 | #if defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)) 11 | __auto_type i = 1; 12 | assert(_Generic((i), int: 1, default: 0)); 13 | #endif 14 | return EXIT_SUCCESS; 15 | } 16 | -------------------------------------------------------------------------------- /gcc/bibliography.md: -------------------------------------------------------------------------------- 1 | # Bibliography 2 | 3 | Extensions: 4 | 5 | - 6 | 7 | Good selection of useful features. 8 | 9 | Internals: 10 | 11 | - 12 | 13 | Generated from `gcc/doc/gccint.texi`, so very official, yay! 14 | 15 | - 16 | 17 | - 18 | -------------------------------------------------------------------------------- /gcc/built-in.md: -------------------------------------------------------------------------------- 1 | # Built-in function 2 | 3 | - https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html 4 | - https://gcc.gnu.org/onlinedocs/gcc/Target-Builtins.html 5 | 6 | Many GCC special functions and macros are prefixed `__builtin_`. 7 | 8 | Those which are ANSI C functions, also have versions without `__builtin_`, but only the `__builtin_` version can be used e.g. for C99 built-ins with `-std=c89`. 9 | -------------------------------------------------------------------------------- /gcc/builtin_constant_p.c: -------------------------------------------------------------------------------- 1 | /* 2 | # builtin_constant_p() 3 | 4 | Returns true iff GCC could determine that the given expression is constant, 5 | to decide if compile time optimizations may be done or not. 6 | 7 | GCC is not smart enough to decide all cases correctly. 8 | 9 | TODO what is a compile time constant? How to use this? 10 | */ 11 | 12 | #include "common.h" 13 | 14 | int main(void) { 15 | assert(__builtin_constant_p(1)); 16 | assert(__builtin_constant_p(1 + 1)); 17 | const int i = 0; 18 | assert(!__builtin_constant_p(i)); 19 | return EXIT_SUCCESS; 20 | } 21 | -------------------------------------------------------------------------------- /gcc/case_range.c: -------------------------------------------------------------------------------- 1 | /* 2 | # case range 3 | */ 4 | 5 | #include "common.h" 6 | 7 | int main(void) { 8 | int i = 1; 9 | switch (i) { 10 | case 0 ... 2: 11 | assert(1); 12 | break; 13 | case 3 ... 5: 14 | assert(0); 15 | break; 16 | default: 17 | assert(0); 18 | break; 19 | } 20 | return EXIT_SUCCESS; 21 | } 22 | -------------------------------------------------------------------------------- /gcc/common.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include /* assert */ 3 | #include /* Complex integer types. */ 4 | #include /* intmax_t */ 5 | #include 6 | #include 7 | #include /* ..., va_list, va_start, va_arg, va_end */ 8 | #include 9 | #include /* EXIT_SUCCESS */ 10 | #include 11 | #include /* time() */ 12 | #include /* execl for sentinel attribute */ 13 | -------------------------------------------------------------------------------- /gcc/complex_integer.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Complex integer. 3 | 4 | C99 has floating point complex numbers, but no integer complex numbers. 5 | */ 6 | 7 | #include "common.h" 8 | 9 | int main(void) { 10 | int complex z = 1 + 1*I; 11 | int complex z2 = 1 - 1*I; 12 | assert(z + z2 == 2 ); 13 | return EXIT_SUCCESS; 14 | } 15 | -------------------------------------------------------------------------------- /gcc/empty_struct.c: -------------------------------------------------------------------------------- 1 | /* Empty struct */ 2 | 3 | #include "common.h" 4 | 5 | int main(void) { 6 | struct s {}; 7 | struct s s0; 8 | assert(sizeof(s0) == 0); 9 | return EXIT_SUCCESS; 10 | } 11 | -------------------------------------------------------------------------------- /gcc/gcc-as-library.md: -------------------------------------------------------------------------------- 1 | # GCC as library 2 | 3 | How to use GCC as a library, e.g. from another C program 4 | 5 | - 6 | - 7 | - 8 | 9 | `libgccjit` from GCC 5 goes a long way. 10 | -------------------------------------------------------------------------------- /gcc/go/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | .PHONY: clean run 3 | 4 | main.out: main.go 5 | # -g is required! http://stackoverflow.com/questions/25081943/no-debug-info-in-elf-executable-errno-when-running-a-binary-built-with-gccgo 6 | gccgo -g -o main.out main.go 7 | # TODO how to do it with GCC directly? How to link? 8 | #gcc-4.9 -c main.go 9 | # 10 | 11 | clean: 12 | rm -f main.out main.o 13 | 14 | run: main.out 15 | ./main.out 16 | -------------------------------------------------------------------------------- /gcc/go/README.md: -------------------------------------------------------------------------------- 1 | # Go 2 | 3 | Go uses `go1` as the internal compiler, analogous to `cc1`. 4 | -------------------------------------------------------------------------------- /gcc/go/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("hello world") 7 | } 8 | -------------------------------------------------------------------------------- /gcc/ide.md: -------------------------------------------------------------------------------- 1 | # IDE 2 | 3 | How to set up some IDEs to develop GCC. 4 | 5 | If only it used CMake, which is able to generate IDE project formats. 6 | 7 | ## Eclipse 8 | 9 | 10 | -------------------------------------------------------------------------------- /gcc/identifiers.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Identifiers 3 | */ 4 | 5 | #include "common.h" 6 | 7 | int main(void) { 8 | /* 9 | # $ is legal in identifiers 10 | 11 | Like in Java and JavaScript. 12 | */ 13 | { 14 | int $ = 0; 15 | assert($ == 0); 16 | } 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /gcc/spec-files.md: -------------------------------------------------------------------------------- 1 | # Spec files TODO 2 | 3 | Determine exactly how GCC calls other programs. 4 | 5 | - 6 | - 7 | 8 | View the current spec file: 9 | 10 | gcc -dumpspecs 11 | 12 | Use given spec file: 13 | 14 | gcc -specs= 15 | -------------------------------------------------------------------------------- /gcc/typeof.c: -------------------------------------------------------------------------------- 1 | /* # typeof 2 | * 3 | * Like C++11 decltype. 4 | * 5 | * Partially reproductible with C11 `_Generic`. 6 | * 7 | * https://stackoverflow.com/questions/6513806/would-it-be-possible-to-add-type-inference-to-the-c-language/31709221#31709221 8 | */ 9 | 10 | #include "common.h" 11 | 12 | int main(void) { 13 | /* Same as: double j = 0.5; */ 14 | typeof(1 + 0.5) j = 0.5; 15 | assert(j == 0.5); 16 | 17 | /* Similar to C++ auto. */ 18 | __auto_type k = 0.5; 19 | assert(sizeof(j) == sizeof(k)); 20 | 21 | return EXIT_SUCCESS; 22 | } 23 | -------------------------------------------------------------------------------- /gcc/weak/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_one -------------------------------------------------------------------------------- /gcc/weak/Makefile_params: -------------------------------------------------------------------------------- 1 | ../Makefile_params -------------------------------------------------------------------------------- /gcc/weak/README.md: -------------------------------------------------------------------------------- 1 | # Weak 2 | 3 | - https://stackoverflow.com/questions/274753/how-to-make-weak-linking-work-with-gcc/54601464#54601464 4 | - https://stackoverflow.com/questions/13089166/how-to-make-gcc-link-strong-symbol-in-static-library-to-overwrite-weak-symbol 5 | -------------------------------------------------------------------------------- /gcc/weak/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int my_weak_var __attribute__((weak)) = 1; 6 | 7 | int main(void) { 8 | printf("%d\n", my_weak_var); 9 | assert(my_weak_var == 2); 10 | return EXIT_SUCCESS; 11 | } 12 | -------------------------------------------------------------------------------- /gcc/weak/notmain.c: -------------------------------------------------------------------------------- 1 | /* Turn this off, and watch the variable change value. */ 2 | #if 1 3 | int my_weak_var = 2; 4 | #endif 5 | -------------------------------------------------------------------------------- /gcc/zero_length_array.c: -------------------------------------------------------------------------------- 1 | /* 2 | # Zero length arrays 3 | 4 | http://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c 5 | 6 | Major application looks like pre-99 flexible array member: 7 | 8 | struct S { 9 | int is[0]; 10 | }; 11 | 12 | to save a byte compared to: 13 | 14 | struct S { 15 | int is[1]; 16 | }; 17 | */ 18 | 19 | #include "common.h" 20 | 21 | int main(void) { 22 | int is[0]; 23 | int i; 24 | printf("&is[0] = %p\n", &is[0]); 25 | printf("&i = %p\n", &i); 26 | return EXIT_SUCCESS; 27 | } 28 | -------------------------------------------------------------------------------- /gcc_std_default/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /gcc_std_default/README.md: -------------------------------------------------------------------------------- 1 | Let's find what is the default value for `-std=`: https://stackoverflow.com/questions/14737104/what-is-the-default-c-mode-for-the-current-gcc-especially-on-ubuntu/53063656#53063656 2 | -------------------------------------------------------------------------------- /gcc_std_default/c.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | #ifdef __STDC_VERSION__ 5 | printf("__STDC_VERSION__ = %ld \n", __STDC_VERSION__); 6 | #endif 7 | #ifdef __STRICT_ANSI__ 8 | puts("__STRICT_ANSI__"); 9 | #endif 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /gcc_std_default/c.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | for std in c89 c99 c11 c17 gnu89 gnu99 gnu11 gnu17; do 3 | echo $std 4 | gcc -std=$std -o c.out c.c 5 | ./c.out 6 | echo 7 | done 8 | echo default 9 | gcc -o c.out c.c 10 | ./c.out 11 | -------------------------------------------------------------------------------- /gcc_std_default/cpp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | #ifdef __cplusplus 5 | std::cout << __cplusplus << std::endl; 6 | #endif 7 | #ifdef __STRICT_ANSI__ 8 | std::cout << "__STRICT_ANSI__" << std::endl; 9 | #endif 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /gcc_std_default/cpp.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | for std in c++98 c++11 c++14 c++17 gnu++98 gnu++11 gnu++14 gnu++17; do 3 | echo $std 4 | g++ -std=$std -o cpp.out cpp.cpp 5 | ./cpp.out 6 | echo 7 | done 8 | echo default 9 | g++ -o cpp.out cpp.cpp 10 | ./cpp.out 11 | -------------------------------------------------------------------------------- /gdb/arg_stdin.c: -------------------------------------------------------------------------------- 1 | /* 2 | - gdb --args arg_stdin.out arg arg1 3 | - set args arg0 arg1 7 | 8 | int main(int argc, char **argv) { 9 | char s[16]; 10 | fgets(s, sizeof(s), stdin); 11 | printf("stdin line 0 = %s", s); 12 | printf("argv[0] = %s\n", argv[0]); 13 | if (argc > 1) { 14 | printf("argv[1] = %s\n", argv[1]); 15 | if (argc > 2) { 16 | printf("argv[2] = %s\n", argv[2]); 17 | } 18 | } 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /gdb/argv-wrapper: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | doc=" 4 | Pass parameters to python script. 5 | 6 | Usage: 7 | 8 | argv-wrapper 0 argv.py 1 2 3 9 | 10 | Where scrpit.py uses the arguments like: 11 | 12 | print(arg0) 13 | print(arg1) 14 | 15 | http://stackoverflow.com/questions/29992153/how-to-pass-arguments-to-a-python-gdb-script-launched-from-command-line/31227632#31227632 16 | " 17 | 18 | py="$1" 19 | shift 20 | cli='' 21 | i=0 22 | for arg in $*; do 23 | cli="$cli -ex 'py arg$i = $arg'" 24 | i=$(($i+1)) 25 | done 26 | eval gdb --batch $cli -x "$py" 27 | -------------------------------------------------------------------------------- /gdb/argv.py: -------------------------------------------------------------------------------- 1 | print(arg0) 2 | print(arg1) 3 | -------------------------------------------------------------------------------- /gdb/array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int is[] = {0, 1, 2, 3}; 4 | enum N { N = 4 }; 5 | int *ip; 6 | 7 | int main(void) { 8 | /* http://stackoverflow.com/questions/1651682/how-to-watch-a-c-dynamic-array-using-gdb */ 9 | ip = is; 10 | return EXIT_SUCCESS; 11 | } 12 | -------------------------------------------------------------------------------- /gdb/array.gdb: -------------------------------------------------------------------------------- 1 | start 2 | print is 3 | search return 4 | until $_ 5 | # Prints the pointer value. 6 | print ip 7 | # Prints the contents. 8 | print *ip@4 9 | -------------------------------------------------------------------------------- /gdb/big_function.c: -------------------------------------------------------------------------------- 1 | /* 2 | For testing how many lines some debugging commands show, e.g. `l` 3 | */ 4 | 5 | #include 6 | 7 | int main(void) { 8 | int i = 0; 9 | i += 1; 10 | i += 2; 11 | i += 3; 12 | i += 4; 13 | i += 5; 14 | i += 6; 15 | i += 7; 16 | i += 8; 17 | i += 9; 18 | i += 10; 19 | i += 11; 20 | i += 12; 21 | i += 13; 22 | i += 14; 23 | i += 15; 24 | i += 16; 25 | i += 17; 26 | i += 18; 27 | i += 19; 28 | i += 20; 29 | printf("%d\n", i); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /gdb/call.c: -------------------------------------------------------------------------------- 1 | /* 2 | - https://stackoverflow.com/questions/1354731/how-to-evaluate-functions-in-gdb 3 | - https://jvns.ca/blog/2018/01/04/how-does-gdb-call-functions/ 4 | */ 5 | 6 | #include 7 | 8 | /* Memory side effects are not reverted. */ 9 | int global = 0; 10 | 11 | int f(int x) { 12 | printf("%d\n", x); 13 | global++; 14 | return x + 1; 15 | } 16 | 17 | int main(void) { 18 | /* Try: 19 | * (gdb) call f(1) 20 | * (gdb) p global 21 | * (gdb) call f(2) 22 | * (gdb) p global 23 | * */ 24 | printf("%d\n", f(1)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /gdb/compile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i = 0; 6 | printf("%d\n", i); 7 | return EXIT_SUCCESS; 8 | } 9 | -------------------------------------------------------------------------------- /gdb/compile.gdb: -------------------------------------------------------------------------------- 1 | start 2 | next 3 | compile code int j = 1; i = j; asm("nop"); asm volatile ("mov $0x123, %rax"); 4 | p "HERE" 5 | p $rcx 6 | p "HERE" 7 | continue 8 | -------------------------------------------------------------------------------- /gdb/continue_return_py.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i; 6 | if (time(NULL) % 2) { 7 | i = 0; 8 | } else { 9 | puts("1"); 10 | return 1; 11 | } 12 | printf("%d", i); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /gdb/count_infinite_test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import common 4 | 5 | t = common.GdbTestcase(__file__) 6 | t.continue_to('inc') 7 | assert t.get_int('i') == 0 8 | t.continue_to('inc') 9 | assert t.get_int('i') == 1 10 | t.destroy() 11 | -------------------------------------------------------------------------------- /gdb/define.c: -------------------------------------------------------------------------------- 1 | /* 2 | # define 3 | 4 | # Macros 5 | 6 | # Preprocessor 7 | 8 | Requires -ggdb3, just -g is not enough. 9 | 10 | TODO confirm: GDB extension to DWARF. 11 | 12 | - http://stackoverflow.com/questions/2934006/how-do-i-print-a-defined-constant-in-gdb 13 | - http://stackoverflow.com/questions/13418634/call-setbufstdout-null-no-symbol-null-in-the-current-context 14 | */ 15 | 16 | /* Contains NULL. */ 17 | #include 18 | 19 | #define ABC 123 20 | 21 | int main(void) { 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /gdb/define.gdb: -------------------------------------------------------------------------------- 1 | # start is required. 2 | start 3 | print ABC 4 | print NULL 5 | -------------------------------------------------------------------------------- /gdb/disas_py.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i; 6 | i = time(NULL); 7 | /* This should generate an inner block. */ 8 | { 9 | int i; 10 | i = time(NULL); 11 | printf("%d\n", i); 12 | } 13 | printf("%d\n", i); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /gdb/disassemble.py: -------------------------------------------------------------------------------- 1 | """ 2 | Disassemble some instructions. 3 | 4 | https://sourceware.org/gdb/onlinedocs/gdb/Architectures-In-Python.html 5 | """ 6 | 7 | gdb.execute('file big_function.out', to_string=True) 8 | gdb.execute('start', to_string=True) 9 | 10 | frame = gdb.selected_frame() 11 | arch = frame.architecture() 12 | print(arch.name()) 13 | 14 | pc = gdb.selected_frame().pc() 15 | 16 | print(arch.disassemble(pc)[0]) 17 | -------------------------------------------------------------------------------- /gdb/dwarf.md: -------------------------------------------------------------------------------- 1 | # DWARF 2 | 3 | Debug information format. 4 | 5 | Implemented by GCC, clang. 6 | 7 | Dominates on Linux. 8 | 9 | 10 | 11 | `dwarfdump` is a tool that can disassemble DWARF information. It uses libdwarf. 12 | 13 | Specs: 14 | -------------------------------------------------------------------------------- /gdb/empty.c: -------------------------------------------------------------------------------- 1 | int main(void) { 2 | return 0; 3 | } 4 | -------------------------------------------------------------------------------- /gdb/event.py: -------------------------------------------------------------------------------- 1 | """ 2 | ## Event 3 | 4 | Simple event example. 5 | 6 | This example shows how to have callbacks on breakpoints 7 | which we did not define with the Breakpoint class. 8 | """ 9 | 10 | def stop_handler(event): 11 | print('stop_handler') 12 | print('stop_signal = ' + str(event.breakpoints)) 13 | gdb.events.stop.connect(stop_handler) 14 | 15 | gdb.execute('file hello_world.out') 16 | gdb.execute('break main') 17 | gdb.execute('run') 18 | -------------------------------------------------------------------------------- /gdb/finish_breakpoint_py.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int g() { 5 | return 1; 6 | } 7 | 8 | int f() { 9 | return g(); 10 | } 11 | 12 | int main(void) { 13 | printf("%d\n", f()); 14 | return EXIT_SUCCESS; 15 | } 16 | -------------------------------------------------------------------------------- /gdb/fork.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define _XOPEN_SOURCE 700 4 | #include /* pid_t */ 5 | #include /* fork */ 6 | 7 | int main(void) { 8 | pid_t pid = fork(); 9 | if (pid == 0) { 10 | puts("child"); 11 | } else { 12 | puts("parent"); 13 | } 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /gdb/fork.gdb: -------------------------------------------------------------------------------- 1 | break main 2 | 3 | set follow-fork-mode parent 4 | run 5 | while 1 6 | step 7 | end 8 | 9 | # TODO would need to either: 10 | 11 | # - ignore the error of doing a `step` after it finishes running 12 | # - check if the program is running on the while 13 | 14 | # for the following code to run. 15 | 16 | #set follow-fork-mode child 17 | #run 18 | #while 1 19 | #next 20 | #end 21 | -------------------------------------------------------------------------------- /gdb/function_pointer.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int f(int i, int j) { 4 | return i + j; 5 | } 6 | 7 | int main(void) { 8 | int i; 9 | int (*fp)(int, int); 10 | fp = f; 11 | i = (*fp)(1, 2); 12 | printf("%d\n", i); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /gdb/function_pointer.gdb: -------------------------------------------------------------------------------- 1 | break f 2 | commands 3 | backtrace 4 | continue 5 | end 6 | run 7 | -------------------------------------------------------------------------------- /gdb/getting-started.md: -------------------------------------------------------------------------------- 1 | # Getting started 2 | 3 | If the program `string.c` has a corresponding `string.gdb` file, you can run it with with: 4 | 5 | make 6 | ./run string 7 | ./run string. 8 | ./run string.c 9 | ./run string.gdb 10 | 11 | We have started automating some tests with pexpect: 12 | 13 | make 14 | ./test 15 | 16 | This semi-duplicates what the GDB tree does with expect :-) 17 | -------------------------------------------------------------------------------- /gdb/hello_world.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | puts("hello world"); 6 | return EXIT_SUCCESS; 7 | } 8 | -------------------------------------------------------------------------------- /gdb/hello_world.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | print('hello world') 4 | -------------------------------------------------------------------------------- /gdb/hello_world_py.gdb: -------------------------------------------------------------------------------- 1 | source hello_world.py 2 | -------------------------------------------------------------------------------- /gdb/help.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | """ 4 | Get information on the inferface. 5 | """ 6 | 7 | help(gdb) 8 | help(gdb.command) 9 | help(gdb.events) 10 | help(gdb.function) 11 | -------------------------------------------------------------------------------- /gdb/if_else.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(void) { 5 | int i; 6 | if (time(NULL) % 2) { 7 | i = 0; 8 | } else { 9 | i = 1; 10 | } 11 | printf("%d", i); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /gdb/maybe_crash.c: -------------------------------------------------------------------------------- 1 | /* 2 | https://stackoverflow.com/questions/27770896/how-to-debug-rare-deadlock 3 | 4 | gdb -ex 'set confirm on' -ex=run -ex=quit --args ./maybe_crash.out 5 | */ 6 | 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | int main(void) { 13 | if (time(NULL) % 2) { 14 | raise(SIGINT); 15 | } 16 | puts("no crash"); 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /gdb/maybe_sleep_forever.c: -------------------------------------------------------------------------------- 1 | /* 2 | https://stackoverflow.com/questions/50066342/how-to-get-a-notification-if-a-given-process-runs-for-longer-than-a-specified-ti 3 | */ 4 | 5 | #define _XOPEN_SOURCE 700 6 | #include 7 | #include 8 | #include 9 | 10 | int main(void) { 11 | if (time(NULL) % 2) { 12 | sleep(3); 13 | } else { 14 | while (1) { 15 | sleep(0x7FFFFFFF); 16 | } 17 | } 18 | return EXIT_SUCCESS; 19 | } 20 | -------------------------------------------------------------------------------- /gdb/method.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Method 3 | */ 4 | 5 | #include 6 | 7 | class MyClass { 8 | public: 9 | int myVar; 10 | int myMethod() { 11 | return myVar + 1; 12 | } 13 | }; 14 | 15 | int main() { 16 | MyClass().myMethod(); 17 | } 18 | -------------------------------------------------------------------------------- /gdb/method.gdb: -------------------------------------------------------------------------------- 1 | # Fails. 2 | #break myMethod 3 | 4 | # Works. 5 | break MyClass::myMethod 6 | 7 | # Also works. 8 | rbreak .*myMethod 9 | 10 | run 11 | 12 | continue 13 | -------------------------------------------------------------------------------- /gdb/multifile/README.md: -------------------------------------------------------------------------------- 1 | # Multifile 2 | 3 | Test GDB with multiple source files and multiple directories. 4 | -------------------------------------------------------------------------------- /gdb/multifile/a.c: -------------------------------------------------------------------------------- 1 | int a_i = 1; 2 | int a_f() { return 1; } 3 | int a_g() { return 1; } 4 | -------------------------------------------------------------------------------- /gdb/multifile/d/a.c: -------------------------------------------------------------------------------- 1 | int d_a_i = 1; 2 | int d_a_f() { return 1; } 3 | int d_a_g() { return 1; } 4 | -------------------------------------------------------------------------------- /gdb/multifile/d/b.c: -------------------------------------------------------------------------------- 1 | int d_b_i = 1; 2 | int d_b_f() { return 1; } 3 | int d_b_g() { return 1; } 4 | -------------------------------------------------------------------------------- /gdb/multifile/e/a.c: -------------------------------------------------------------------------------- 1 | int e_a_i = 1; 2 | int e_a_f() { return 1; } 3 | int e_a_g() { return 1; } 4 | -------------------------------------------------------------------------------- /gdb/multiple_python_invocations.gdb: -------------------------------------------------------------------------------- 1 | # A single interpreter is used for all invocations. 2 | py a = 0 3 | py assert a == 0 4 | -------------------------------------------------------------------------------- /gdb/nested.c: -------------------------------------------------------------------------------- 1 | /* 2 | Stack trace tests: bt, f, whe 3 | */ 4 | 5 | #include 6 | 7 | int f0(int i) { 8 | return i + 1; 9 | } 10 | 11 | int f1(int i) { 12 | return f0(i + 1); 13 | } 14 | 15 | int f2(int i) { 16 | return f1(i + 1); 17 | } 18 | 19 | int f3(int i) { 20 | return f2(i + 1); 21 | } 22 | 23 | int main(void) { 24 | printf("%d\n", f3(0)); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /gdb/nexti.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int f() { 4 | puts("in f"); 5 | } 6 | 7 | int main(void) { 8 | /* Do a nexti now! */ 9 | f(); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /gdb/overload.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | b f 3 | 4 | will add breakpoints to both overloads. 5 | 6 | To differentiate between them, use: 7 | 8 | b f(int) 9 | b f(float) 10 | */ 11 | 12 | #include 13 | 14 | void f(int i) { 15 | std::cout << "int" << std::endl; 16 | } 17 | 18 | void f(float f) { 19 | std::cout << "float" << std::endl; 20 | } 21 | 22 | int main() { 23 | f(1); 24 | f(1.0f); 25 | } 26 | -------------------------------------------------------------------------------- /gdb/read_var.py: -------------------------------------------------------------------------------- 1 | """ 2 | ## read_var 3 | 4 | Get the current value of a variable as a Value object. 5 | 6 | To loop over all variables, use blocks. 7 | """ 8 | 9 | gdb.execute('file read_var_py.out', to_string=True) 10 | gdb.execute('start', to_string=True) 11 | frame = gdb.selected_frame() 12 | print(frame.read_var('i')) 13 | print(frame.read_var('g')) 14 | print(frame.read_var('f')) 15 | print() 16 | for i in range(5): 17 | gdb.execute('next', to_string=True) 18 | print(frame.read_var('i')) 19 | print(frame.read_var('g')) 20 | print(frame.read_var('f')) 21 | -------------------------------------------------------------------------------- /gdb/read_var_py.c: -------------------------------------------------------------------------------- 1 | /* 2 | Local and global variables. 3 | */ 4 | 5 | #include 6 | 7 | int g = 1; 8 | 9 | int main(void) { 10 | int i; 11 | float f; 12 | i = 1; 13 | f = 1.5; 14 | g++; 15 | i++; 16 | f += 1.0; 17 | printf("%d %d %f", g, i, f); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /gdb/requirements.txt: -------------------------------------------------------------------------------- 1 | pexpect==4.2.1 2 | -------------------------------------------------------------------------------- /gdb/reverse.gdb: -------------------------------------------------------------------------------- 1 | 2 | start 3 | record 4 | # TODO same as target record-full ? 5 | 6 | continue 7 | reverse-continue 8 | 9 | next 10 | -------------------------------------------------------------------------------- /gdb/run: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | f="${1%.*}" 4 | g="${f}.gdb" 5 | if [ -f "$g" ]; then 6 | gdbcmd="-batch -x $g" 7 | else 8 | gdbcmd= 9 | fi 10 | gdb -nh -q $gdbcmd "${f}.out" 11 | -------------------------------------------------------------------------------- /gdb/segv.c: -------------------------------------------------------------------------------- 1 | /* 2 | Let's take a look at what happens on a segmentation fault. 3 | */ 4 | 5 | int main(void) { 6 | (*((char*)0))++; 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /gdb/si_all.gdb: -------------------------------------------------------------------------------- 1 | # 4k lines on a hello world! 2 | break _start 3 | run 4 | display/1i $pc 5 | while 1 6 | si 7 | end 8 | -------------------------------------------------------------------------------- /gdb/sleep.c: -------------------------------------------------------------------------------- 1 | #define _XOPEN_SOURCE 700 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | sleep(3); 7 | return EXIT_SUCCESS; 8 | } 9 | -------------------------------------------------------------------------------- /gdb/smart_pointer.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | https://stackoverflow.com/questions/22798601/how-to-debug-c11-code-with-unique-ptr-in-ddd-or-gdb 3 | */ 4 | 5 | #include // unique_ptr 6 | 7 | class MyClass { 8 | public: 9 | int myMethod() { 10 | return 1; 11 | } 12 | }; 13 | 14 | int main() { 15 | std::unique_ptr p(new MyClass()); 16 | p->myMethod(); 17 | } 18 | -------------------------------------------------------------------------------- /gdb/stderr_ignore.c: -------------------------------------------------------------------------------- 1 | /* 2 | http://stackoverflow.com/questions/2388561/block-output-of-debugged-program-gdb/35460141#35460141 3 | */ 4 | 5 | #include 6 | #include 7 | 8 | int main(void) { 9 | fprintf(stderr, "hello world\n"); 10 | return EXIT_SUCCESS; 11 | } 12 | -------------------------------------------------------------------------------- /gdb/stderr_ignore.gdb: -------------------------------------------------------------------------------- 1 | # Shows stderr. 2 | run 3 | 4 | # Shows stderr. 5 | run >/dev/null 6 | 7 | # Hides stderr. 8 | run &>/dev/null 9 | -------------------------------------------------------------------------------- /gdb/stdout.gdb: -------------------------------------------------------------------------------- 1 | start 2 | define stdout 3 | catch syscall write 4 | commands 5 | printf "rsi = %s\n", $rsi 6 | bt 7 | continue 8 | end 9 | condition $bpnum $rdi == 1 && strstr((char *)$rsi, "$arg0") != NULL 10 | end 11 | stdout qwer 12 | i b 13 | continue 14 | -------------------------------------------------------------------------------- /gdb/step_all.gdb: -------------------------------------------------------------------------------- 1 | # Step through every line of the program. 2 | # 3 | # Deficiencies: 4 | # 5 | # - The problem with this is that it generates a trace too large, 6 | # specially as it enters every glibc call. 7 | # 8 | # We should develop a way to only enter functions of the program being studied. 9 | # 10 | # - Big boring loops are a problem. 11 | # 12 | # - The while 1 blows at the end of the program. 13 | # 14 | # Python scripting is likely the only way to get around those problems.s 15 | 16 | break main 17 | run 18 | while 1 19 | step 20 | end 21 | -------------------------------------------------------------------------------- /gdb/string.c: -------------------------------------------------------------------------------- 1 | /* 2 | String. 3 | 4 | - http://stackoverflow.com/questions/1530736/how-to-print-a-null-terminated-string-with-newlines-without-showing-backslash-es 5 | - http://stackoverflow.com/questions/2821033/assembly-gdb-print-string 6 | - http://stackoverflow.com/questions/12758217/printing-string-pointed-to-from-register-in-gdb 7 | */ 8 | 9 | #include 10 | 11 | char *s = "ab\ncd\n"; 12 | 13 | int main(void) { 14 | printf("%s\n", s); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /gdb/string.gdb: -------------------------------------------------------------------------------- 1 | break main 2 | commands 3 | x/s s 4 | print/s s 5 | printf "%s", s 6 | call printf("%s", s) 7 | continue 8 | end 9 | run 10 | -------------------------------------------------------------------------------- /gdb/struct.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct { 4 | int i; 5 | float f; 6 | } S; 7 | 8 | S s = {1, 1.1}; 9 | 10 | int main(void) { 11 | printf("%d %f\n", s.i, s.f); 12 | s.i = 2; 13 | s.f = 2.2; 14 | s.i = 3; 15 | s.f = 3.3; 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /gdb/struct.gdb: -------------------------------------------------------------------------------- 1 | break main 2 | run 3 | print s 4 | -------------------------------------------------------------------------------- /gdb/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | for t in *_test.py; do 3 | ./"$t" 4 | done 5 | -------------------------------------------------------------------------------- /gdb/trace.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int f(int i) { 5 | return i + 1; 6 | } 7 | 8 | int main(void) { 9 | int i; 10 | for (i = 0; i < 4; i++) { 11 | printf("%d\n", f(i)); 12 | } 13 | return EXIT_SUCCESS; 14 | } 15 | -------------------------------------------------------------------------------- /gdb/trace.gdb: -------------------------------------------------------------------------------- 1 | trace f 2 | actions 3 | collect $args 4 | end 5 | tstart 6 | run 7 | tfind start 8 | while ($trace_frame != -1) 9 | tdump 10 | tfind 11 | end 12 | -------------------------------------------------------------------------------- /gdb/trace.md: -------------------------------------------------------------------------------- 1 | # trace 2 | 3 | # Tracepoint 4 | 5 | Only works for remote. If you try it out you get: 6 | 7 | You can't do that when your target is `exec' 8 | 9 | TODO. Vs breakpoint? More restricted. Is it considerably faster? If so, why? Hardware support? Or is it just faster for remote debugging? How is it implemented? 10 | -------------------------------------------------------------------------------- /gdb/value.py: -------------------------------------------------------------------------------- 1 | """ 2 | ## Value 3 | 4 | Let's explore Value objects. 5 | 6 | They are returned by: 7 | 8 | - gdb.parse_and_eval 9 | - gdb.read_var 10 | """ 11 | 12 | gdb.execute('set $a = 12') 13 | gdb.execute('set $b = 3.4') 14 | 15 | # Returned by parse_and_eval 16 | a = gdb.parse_and_eval('$a') 17 | b = gdb.parse_and_eval('$b') 18 | 19 | # Return Value objects. 20 | print(type(a)) 21 | print(type(b)) 22 | 23 | print(a.type) 24 | print(b.type) 25 | 26 | # str is defined 27 | print(a) 28 | print(b) 29 | 30 | # Conversion to scalars are defined. 31 | print(a + 1) 32 | print(b + 1.0) 33 | -------------------------------------------------------------------------------- /gdb/visible_variables_py.c: -------------------------------------------------------------------------------- 1 | int i = 0; 2 | int j = 0; 3 | int k = 0; 4 | 5 | int main(int argc, char **argv) { 6 | int i = 1; 7 | int j = 1; 8 | { 9 | int i = 2; 10 | i = 3; 11 | i = 4; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /gdb/watch.c: -------------------------------------------------------------------------------- 1 | /* 2 | For testing how many lines some debugging commands show, e.g. `l` 3 | */ 4 | 5 | #include 6 | 7 | int i = 0; 8 | 9 | int main(void) { 10 | i += 1; 11 | i += 2; 12 | i += 3; 13 | printf("%d\n", i); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /gdb/watch.gdb: -------------------------------------------------------------------------------- 1 | watch i 2 | commands 3 | continue 4 | end 5 | run 6 | -------------------------------------------------------------------------------- /glibc/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /glibc/gnu_get_libc_version.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | 5 | int main(void) { 6 | printf("gnu_get_libc_version() = %s\n", gnu_get_libc_version()); 7 | } 8 | -------------------------------------------------------------------------------- /glibc/interactive/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /glibc/interactive/common.h: -------------------------------------------------------------------------------- 1 | ../common.h -------------------------------------------------------------------------------- /glibc/libc_main.c: -------------------------------------------------------------------------------- 1 | /* 2 | Being naughty. 3 | 4 | __libc_start_main is reserved by ANSI C, so its fine. 5 | 6 | _start is not? 7 | */ 8 | /*void __libc_start_main() {}*/ 9 | /*void _start() {}*/ 10 | 11 | int main(void) { 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /glibc/strfry.c: -------------------------------------------------------------------------------- 1 | /* # strfry 2 | * 3 | * Randomly shuffle a string. 4 | * 5 | * Return the string itself. 6 | * 7 | * C++ has std::shuffle. 8 | * 9 | * TODO mnemonic? Fry a string? 10 | */ 11 | 12 | #include "common.h" 13 | 14 | int main(void) { 15 | char s[] = "abcd"; 16 | printf("%s\n", strfry(s)); 17 | printf("%s\n", strfry(s)); 18 | } 19 | -------------------------------------------------------------------------------- /gsl/.gitignore: -------------------------------------------------------------------------------- 1 | *.png 2 | -------------------------------------------------------------------------------- /gsl/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /gsl/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lgsl -lgslcblas -lm -lplplot 2 | -------------------------------------------------------------------------------- /gsl/README.md: -------------------------------------------------------------------------------- 1 | # GSL 2 | 3 | Gnu Scientific Library. 4 | 5 | 6 | 7 | 1. [ODE](ode.md) 8 | 1. [Bessel](bessel.c) 9 | -------------------------------------------------------------------------------- /gsl/bessel.c: -------------------------------------------------------------------------------- 1 | #include "common.h" 2 | 3 | int main(void) { 4 | double x = 5.0; 5 | printf("J0(%g) = %.18e\n", x, gsl_sf_bessel_J0(x)); 6 | return EXIT_SUCCESS; 7 | } 8 | -------------------------------------------------------------------------------- /gsl/common.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_H 2 | #define COMMON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /gsl/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install \ 4 | libgsl0-dev \ 5 | libplplot-dev \ 6 | plplot-driver-xwin \ 7 | ; 8 | -------------------------------------------------------------------------------- /gsl/ode.gnuplot: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env gnuplot 2 | set terminal png size 1024, 1024 3 | set output "ode.png" 4 | set multiplot layout 3,1 title "ODE solution" 5 | 6 | set title "Solution: t x g(t) " 7 | set xlabel "t" 8 | set ylabel "g(t)" 9 | plot "ode.dat" using 1:2 notitle 10 | 11 | set title "Derivative plot: t x g'(t)" 12 | set xlabel "t" 13 | set ylabel "g'(t)" 14 | plot "ode.dat" using 1:3 notitle 15 | 16 | set title "Phase space: g(t) x g(t)" 17 | set xlabel "g'(t)" 18 | set ylabel "g(t)" 19 | plot "ode.dat" using 2:3 notitle 20 | -------------------------------------------------------------------------------- /gtk/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /gtk/Makefile_params: -------------------------------------------------------------------------------- 1 | PKGNAME := gtk+-3.0 2 | CFLAGS := $(shell pkg-config --cflags "$(PKGNAME)") 3 | LIBS := $(shell pkg-config --libs "$(PKGNAME)") 4 | RUN := min 5 | -------------------------------------------------------------------------------- /gtk/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libgtk-3-dev 4 | -------------------------------------------------------------------------------- /gtk/min.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main (int argc, char *argv[]) 4 | { 5 | GtkWidget *window; 6 | 7 | gtk_init (&argc, &argv); 8 | 9 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); 10 | 11 | g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); 12 | 13 | gtk_widget_show (window); 14 | 15 | gtk_main (); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /kde/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project(kdecheat) 2 | find_package(KDE4 REQUIRED) 3 | include_directories(${KDE4_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR}) 4 | set(kdecheat_SRCS main.cpp) 5 | kde4_add_ui_files(kdecheat_SRCS general.ui) 6 | kde4_add_kcfg_files(kdecheat_SRCS settings.kcfgc) 7 | kde4_add_executable(kdecheat ${kdecheat_SRCS}) 8 | target_link_libraries(kdecheat ${KDE4_KDEUI_LIBS}) 9 | install(FILES kdecheatui.rc DESTINATION ${DATA_INSTALL_DIR}/kdecheat) 10 | install(FILES kdecheat.kcfg DESTINATION ${KCFG_INSTALL_DIR}) 11 | install(TARGETS kdecheat DESTINATION ${BIN_INSTALL_DIR}) 12 | -------------------------------------------------------------------------------- /kde/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get install kdelibs5-dev 3 | -------------------------------------------------------------------------------- /kde/settings.kcfgc: -------------------------------------------------------------------------------- 1 | File=kdecheat.kcfg 2 | ClassName=Settings 3 | Singleton=true 4 | Mutators=true 5 | -------------------------------------------------------------------------------- /lapack/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /lapack/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lm -lblas -llapacke 2 | FFLIBS := -l:libblas/libblas.so -l:lapack/liblapack.so 3 | -------------------------------------------------------------------------------- /lapack/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | # BLAS C/Fotran and LAPACK Fortran: 4 | sudo apt-get install liblapack-dev liblapacke-dev 5 | # Lapack C via LAPACKE: 6 | # TODO: how to install on Ubuntu 12.04? 7 | # The following works only on later Ubuntu 8 | #sudo apt-get install liblapacke-dev 9 | -------------------------------------------------------------------------------- /lapack/test: -------------------------------------------------------------------------------- 1 | ../test_many -------------------------------------------------------------------------------- /linker/gold_benchmark/.gitignore: -------------------------------------------------------------------------------- 1 | *.c 2 | *.h 3 | *.log 4 | /main 5 | -------------------------------------------------------------------------------- /linker/gold_benchmark/README.md: -------------------------------------------------------------------------------- 1 | # Gold benchmark 2 | 3 | https://stackoverflow.com/questions/3476093/replacing-ld-with-gold-any-experience/53921263#53921263 4 | 5 | Usage: 6 | 7 | ./bench-all 8 | cat results.log 9 | -------------------------------------------------------------------------------- /linker/gold_benchmark/bench-all: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | result_log=result.log 4 | rm -f "$result_log" 5 | for params in \ 6 | "10000 10 10" \ 7 | "1000 100 10" \ 8 | "100 1000 10" \ 9 | "10000 10 100" \ 10 | "1000 100 100" \ 11 | "100 1000 100" \ 12 | ; do 13 | echo $params | tee -a "$result_log" 14 | ./generate-objects $params 15 | ./bench "${params}.log" 16 | cat "${params}.log" >> "$result_log" 17 | echo >> "$result_log" 18 | echo 19 | done 20 | -------------------------------------------------------------------------------- /linker/gold_benchmark/clean: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | ./clean-build 3 | rm -f *.log 4 | -------------------------------------------------------------------------------- /linker/gold_benchmark/clean-build: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo *.c *.h *.o main | xargs rm -f 3 | -------------------------------------------------------------------------------- /linker/incremental_link/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all clean test 2 | 3 | CFLAGS = -ggdb3 -std=c99 -O0 4 | 5 | all: main.out main_r.out 6 | 7 | main.out: f12.o main.o 8 | $(CC) $(CFLAGS) -o '$@' $^ 9 | 10 | main_r.out: f12_r.o main.o 11 | $(CC) $(CFLAGS) -o '$@' $^ 12 | 13 | f12_r.o: f1.o f2.o 14 | ld -o $@ -r $^ 15 | 16 | clean: 17 | rm -f *.o *.out 18 | 19 | test: all 20 | ./main.out 21 | ./main_r.out 22 | -------------------------------------------------------------------------------- /linker/incremental_link/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/29391965/what-is-partial-linking-in-gnu-linker/53959624#53959624 2 | -------------------------------------------------------------------------------- /linker/incremental_link/f1.c: -------------------------------------------------------------------------------- 1 | #include "f1.h" 2 | -------------------------------------------------------------------------------- /linker/incremental_link/f1.h: -------------------------------------------------------------------------------- 1 | int f_2(void); 2 | 3 | int f_1_2(void) { 4 | return f_2() + 1; 5 | } 6 | 7 | int f_1(void) { 8 | return 1; 9 | } 10 | -------------------------------------------------------------------------------- /linker/incremental_link/f12.c: -------------------------------------------------------------------------------- 1 | #include "f1.h" 2 | #include "f2.h" 3 | -------------------------------------------------------------------------------- /linker/incremental_link/f2.c: -------------------------------------------------------------------------------- 1 | #include "f2.h" 2 | -------------------------------------------------------------------------------- /linker/incremental_link/f2.h: -------------------------------------------------------------------------------- 1 | int f_1(void); 2 | 3 | int f_2_1(void) { 4 | return f_1() + 1; 5 | } 6 | 7 | int f_2(void) { 8 | return 2; 9 | } 10 | -------------------------------------------------------------------------------- /linker/incremental_link/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int f_1_2(void); 5 | int f_2_1(void); 6 | 7 | int main(void) { 8 | assert(f_1_2() + f_2_1() == 5); 9 | return EXIT_SUCCESS; 10 | } 11 | -------------------------------------------------------------------------------- /linker/variable_address/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean test 2 | 3 | main.out: main.c link.ld 4 | gcc -fno-pie -no-pie -o '$@' -std=c99 -Wall -Wextra -pedantic $^ 5 | 6 | test: main.out 7 | [ "$$(./'$<')" = "$$(printf 'adr 0x12345678\nval 0x9abcdef0\nval 0x0')" ] 8 | 9 | clean: 10 | rm -f main.out 11 | -------------------------------------------------------------------------------- /linker/variable_address/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/4067811/how-to-place-a-variable-at-a-given-absolute-address-in-memory-with-gcc/54128613#54128613 2 | -------------------------------------------------------------------------------- /linker/variable_address/link.ld: -------------------------------------------------------------------------------- 1 | SECTIONS 2 | { 3 | .mySegment 0x12345678 : {KEEP(*(.mySection))} 4 | } 5 | -------------------------------------------------------------------------------- /linker/variable_address/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int __attribute__((section(".mySection"))) myvar = 0x9ABCDEF0; 4 | 5 | int main(void) { 6 | printf("adr %p\n", (void*)&myvar); 7 | printf("val 0x%x\n", myvar); 8 | myvar = 0; 9 | printf("val 0x%x\n", myvar); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /linux/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /linux/README.md: -------------------------------------------------------------------------------- 1 | # Linux 2 | 3 | Programs that use userland exported Linux kernel APIs, e.g. `/dev` filesystem. 4 | 5 | Syscalls may be included here if they don't have glibc wrappers. 6 | -------------------------------------------------------------------------------- /linux/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libXXX-dev 4 | -------------------------------------------------------------------------------- /make/.gitignore: -------------------------------------------------------------------------------- 1 | tmp.* 2 | *.tmp 3 | *.tmp2 4 | -------------------------------------------------------------------------------- /make/automatic-variables.mk: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | ## Automatic variables. 4 | 5 | # https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html 6 | 7 | ## $@ 8 | 9 | # Target. 10 | 11 | ## $< 12 | 13 | # First dependency. 14 | 15 | ## $^ 16 | 17 | # All dependencies. 18 | 19 | a: b c 20 | [ '$@' = 'a' ] 21 | [ '$<' = 'b' ] 22 | [ '$^' = 'b c' ] 23 | 24 | b: 25 | 26 | c: 27 | -------------------------------------------------------------------------------- /make/bibliography.md: -------------------------------------------------------------------------------- 1 | # Bibliography 2 | 3 | - 4 | 5 | Good first tutorial to make. 6 | 7 | - 8 | -------------------------------------------------------------------------------- /make/builtin_functions/README.md: -------------------------------------------------------------------------------- 1 | # Built-in functions 2 | -------------------------------------------------------------------------------- /make/default_targets/.gitignore: -------------------------------------------------------------------------------- 1 | c 2 | cpp 3 | -------------------------------------------------------------------------------- /make/default_targets/README.md: -------------------------------------------------------------------------------- 1 | Some rules are automatically defined. 2 | 3 | They can be run even if you don't have a makefile in the current directory. 4 | 5 | This can be a convenient way of compiling simple test files, 6 | but beware if make is not going to do things which you did not intend it to do behind your back! 7 | -------------------------------------------------------------------------------- /make/default_targets/c.c: -------------------------------------------------------------------------------- 1 | #include "stdio.h" 2 | #include "stdlib.h" 3 | 4 | int main( int argc, char** argv ) 5 | { 6 | puts( "c" ); 7 | return EXIT_SUCCESS; 8 | } 9 | -------------------------------------------------------------------------------- /make/default_targets/clean.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | rm c 4 | rm cpp 5 | -------------------------------------------------------------------------------- /make/default_targets/cpp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main( int argc, char** argv ) 5 | { 6 | std::cout << "cpp" << std::endl; 7 | return EXIT_SUCCESS; 8 | } 9 | -------------------------------------------------------------------------------- /make/default_targets/make.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | make c 4 | make cpp 5 | -------------------------------------------------------------------------------- /make/environment-variables.mk: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | .PHONY: all 4 | 5 | all: 6 | @echo '$(my_var)' 7 | -------------------------------------------------------------------------------- /make/include/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | ## include 4 | 5 | # Copy paste given file here. 6 | 7 | # Both rules and definitions can be included. 8 | 9 | # Sources another makefile here like shell `.` (dot) 10 | 11 | include a.makefile 12 | include b.makefile 13 | 14 | # If the file does not exist, generate an error: 15 | 16 | #include idontexist 17 | 18 | # Ignore the error if the file does not exist: 19 | 20 | -include idontexist-but-you-ignore-me 21 | -------------------------------------------------------------------------------- /make/include/a.makefile: -------------------------------------------------------------------------------- 1 | a := b 2 | -------------------------------------------------------------------------------- /make/include/b.makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all 2 | 3 | all: 4 | echo 'a = $(a)' 5 | -------------------------------------------------------------------------------- /make/multifile/README.md: -------------------------------------------------------------------------------- 1 | Concepts involving multiple makefiles. 2 | 3 | How to call one makefile from another is called submake on the manual. 4 | -------------------------------------------------------------------------------- /make/multifile/d/Makefile: -------------------------------------------------------------------------------- 1 | .PHONY: all 2 | 3 | all: 4 | [ '$A' = 'a' ] 5 | [ -z '$B' ] 6 | @echo d 7 | @pwd 8 | -------------------------------------------------------------------------------- /make/multiline-variables.mk: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | # This does not work: unquoted variable error. 4 | multiline_inner := multiline_2 5 | define multiline = 6 | @echo multiline_1 7 | @echo $(multiline_inner) 8 | endef 9 | 10 | .PHONY: all 11 | 12 | all: 13 | @# [ "$(multiline_TODO)" = "$$(printf '1\n2)"' ] 14 | -------------------------------------------------------------------------------- /make/plus_sign/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | ## + 4 | 5 | ## plus sign prefix 6 | 7 | # Run command even if `-n`, `-q`, `-t` are passed on the command line. 8 | 9 | # TODO: not running with `-q`? 10 | 11 | .PHONY: all 12 | 13 | all: 14 | echo $cmd 15 | echo 'no plus' 16 | +echo 'plus' 17 | -------------------------------------------------------------------------------- /make/plus_sign/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | echo '## default' 3 | make 4 | echo 5 | echo '## -n' 6 | make -n 7 | echo 8 | echo '## -q' 9 | make -q 10 | echo 11 | echo '## -t' 12 | make -t 13 | -------------------------------------------------------------------------------- /make/precious/Makefile: -------------------------------------------------------------------------------- 1 | .PRECIOUS: a.tmp2 2 | .PHONY: clean 3 | 4 | %.tmp: b.tmp2 5 | touch a.tmp 6 | 7 | %.tmp2: 8 | touch a.tmp2 9 | 10 | clean: 11 | rm -f *.tmp *.tmp2 12 | -------------------------------------------------------------------------------- /make/precious/README.md: -------------------------------------------------------------------------------- 1 | # PRECIOUS 2 | 3 | POSIX 7. 4 | 5 | Special target. 6 | 7 | Keeps given intermediate build files. 8 | 9 | Test: 10 | 11 | make a.tmp 12 | 13 | With precious: both `a.tmp` and `a.tmp2` are created. 14 | 15 | Without: `a.tmp2` is created but gets removed afterwards. You can observe this with `make --dry-run a.tmp` which shows: 16 | 17 | touch a.tmp2 18 | touch a.tmp 19 | rm b.tmp2 20 | 21 | TODO understand better with more examples. 22 | -------------------------------------------------------------------------------- /make/recipes/README.md: -------------------------------------------------------------------------------- 1 | The part that says what each rule will do. 2 | -------------------------------------------------------------------------------- /make/recipes/interpreter/Makefile: -------------------------------------------------------------------------------- 1 | SHELL := /usr/bin/python 2 | 3 | 0: 4 | print 0 5 | 6 | 1: 7 | a = 1;\ 8 | print a 9 | -------------------------------------------------------------------------------- /make/recipes/interpreter/README.md: -------------------------------------------------------------------------------- 1 | Which program will be used to run the recipes 2 | 3 | Make uses `$SHELL` var (makefile var) to determine the interpreter to use 4 | 5 | Reminders: 6 | 7 | - each recipe line means one separated shell invocation 8 | - recipe lines ending in `\\` get following line appended and are passed together to the interpreter #**with** the `\\` 9 | 10 | Default: `sh` 11 | -------------------------------------------------------------------------------- /make/recipes/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | targets=" 4 | separate-invocations 5 | dollar 6 | multiline-command 7 | dollar 8 | no-at 9 | at 10 | no-hyphen 11 | hyphen 12 | at-hyphen 13 | " 14 | 15 | for target in $targets; do 16 | printf "\033[1;31m## $target\033[0m\n" 17 | make "$target" 18 | echo 19 | done 20 | -------------------------------------------------------------------------------- /make/rule/README.md: -------------------------------------------------------------------------------- 1 | # Rule 2 | 3 | Rules say: 4 | 5 | - I depend on another rule 6 | - what recipe to do for me 7 | 8 | Keep in mind the rule syntax and the name of each part of a rule: 9 | 10 | targets : prerequisites 11 | recipe 12 | ... 13 | -------------------------------------------------------------------------------- /make/rule/redefine/Makefile: -------------------------------------------------------------------------------- 1 | # Redefining a target GNU make ignores the first, and gives a warning. 2 | 3 | # It seems that POSIX says nothing about this, so the behaviour is undefined. 4 | 5 | a: 6 | echo a0 7 | 8 | a: 9 | echo a1 10 | 11 | ## Double colon suffix 12 | 13 | ## :: 14 | 15 | # GNU extension. 16 | 17 | # A target with double colon can be redefined without error. 18 | 19 | # Both targets are executed, first the first one to be defined, and then the other one. 20 | 21 | b:: 22 | echo b0 23 | 24 | b:: 25 | echo b1 26 | -------------------------------------------------------------------------------- /make/rule/redefine/README.md: -------------------------------------------------------------------------------- 1 | Redefining a target GNU make ignores the first, and gives a warning. 2 | 3 | It seems that POSIX says nothing about this, so the behaviour is undefined. 4 | -------------------------------------------------------------------------------- /make/rule/timestamp/.gitignore: -------------------------------------------------------------------------------- 1 | /in 2 | /out 3 | /nodep 4 | /phony 5 | -------------------------------------------------------------------------------- /make/rule/timestamp/README.md: -------------------------------------------------------------------------------- 1 | Rules are only made if inputs have been changed after the outputs. 2 | 3 | Make determines this by comparing the time of last modification timestamp of inputs and outputs. 4 | 5 | # PHONY 6 | 7 | Dependencies are understood to be files unless they are put under the `.PHONY` special target. 8 | -------------------------------------------------------------------------------- /make/rule/timestamp/test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ## in out 4 | 5 | #create `in`: 6 | make change_in 7 | #create `out`: 8 | make out 9 | #nothing happens because `in` is not newer than out: 10 | make out 11 | make change_in 12 | #out happens because `in` is newer than `out`: 13 | make out 14 | make clean 15 | 16 | ## nodep 17 | 18 | #runs: 19 | make nodep 20 | make change_nodep 21 | #does not run because `nodep` exists: 22 | make nodep 23 | make clean 24 | 25 | ## phony 26 | 27 | #runs: 28 | make phony 29 | make change_phony 30 | #also runs, because phony is PHONY: 31 | make phony 32 | make clean 33 | -------------------------------------------------------------------------------- /make/target-specific-variable.mk: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | # Set a variable for a single target. 4 | 5 | # TODO check: It is only possible to set one per line. 6 | 7 | # TODO check: not possible to define on the same line 8 | # as the actual rule to avoid repeating the target. 9 | # But we could work around that by storing the target in a variable. 10 | 11 | x := 0 12 | 13 | .PHONY: a 14 | 15 | a: x := 1 16 | a: 17 | @[ '$x' = '1' ] 18 | 19 | b: 20 | @[ '$x' = '0' ] 21 | -------------------------------------------------------------------------------- /make/template.mk: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | .PHONY: all 4 | 5 | all: 6 | -------------------------------------------------------------------------------- /make/templates/README.md: -------------------------------------------------------------------------------- 1 | # Templates 2 | 3 | Quick starting points for new projects. 4 | 5 | Naming conventions: 6 | 7 | - `*-single`: compile a single file into a single executable 8 | - `*-many`: compile every file in the directory to a separate executable 9 | - `*-one`: compile multiple input files into a single executable 10 | -------------------------------------------------------------------------------- /make/templates/c_many/.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | -------------------------------------------------------------------------------- /make/templates/c_many/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("a"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /make/templates/c_many/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("main"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /make/templates/c_many/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | out_ext="$1" 4 | for t in *"$out_ext"; do 5 | if ! ./"$t"; then 6 | echo "ASSERT FAILED: $t" 7 | exit 1 8 | fi 9 | done 10 | 11 | echo 'ALL ASSERTS PASSED' 12 | -------------------------------------------------------------------------------- /make/templates/c_one/.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | -------------------------------------------------------------------------------- /make/templates/c_one/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a() { 4 | return 1; 5 | } 6 | -------------------------------------------------------------------------------- /make/templates/c_one/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int a(); 4 | 5 | int main(void) { 6 | printf("%d\n", a()); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /make/templates/c_single/.gitignore: -------------------------------------------------------------------------------- 1 | *.out 2 | -------------------------------------------------------------------------------- /make/templates/c_single/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | .PHONY: clean run 3 | 4 | main.out: main.c 5 | gcc -pedantic-errors -std=c89 -Wall -o '$@' '$<' 6 | 7 | clean: 8 | rm -f main.out 9 | 10 | run: main.out 11 | ./'$<' 12 | -------------------------------------------------------------------------------- /make/templates/c_single/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("main"); 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /make/templates/fortran-one.makefile: -------------------------------------------------------------------------------- 1 | EXT ?= f 2 | RUN ?= main 3 | 4 | .PROXY: clean run 5 | 6 | main: main.f 7 | gfortran -o '$(RUN)' '$(RUN).$(EXT)' 8 | 9 | clean: 10 | rm '$(RUN)' 11 | 12 | run: $(RUN) 13 | ./'$(RUN)' 14 | -------------------------------------------------------------------------------- /make/templates/java/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | -------------------------------------------------------------------------------- /make/templates/java/B.java: -------------------------------------------------------------------------------- 1 | public final class B { 2 | public static void main(String[] args) { 3 | System.out.println("B"); 4 | System.out.println(C.c()); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /make/templates/java/C.java: -------------------------------------------------------------------------------- 1 | public final class C { 2 | public static String c() { 3 | return "C"; 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /make/templates/java/Main.java: -------------------------------------------------------------------------------- 1 | public final class Main { 2 | public static void main(String[] args) { 3 | System.out.println("Main"); 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /make/templates/java/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | RUN ?= Main 4 | 5 | .PHONY: all clean run 6 | 7 | all: 8 | javac *.java 9 | 10 | clean: 11 | rm -f *.class 12 | 13 | run: all 14 | java -ea '$(RUN)' 15 | -------------------------------------------------------------------------------- /make/templates/java/README.md: -------------------------------------------------------------------------------- 1 | # Java 2 | 3 | javac already: 4 | 5 | - compiles only if necessary based on timestamps. 6 | - takes care of imports for us 7 | 8 | So this simple Makefile deals with all our use cases. 9 | -------------------------------------------------------------------------------- /make/templates/nasm-single-dry.makefile: -------------------------------------------------------------------------------- 1 | NASM ?= nasm -w+all -f $(shell if [ "`uname -m`" = 'x86_64' ]; then printf 'elf64'; else printf 'elf32'; fi ) 2 | TMP_EXT ?= .o 3 | OUT_EXT ?= .out 4 | RUN ?= main 5 | 6 | OUT := $(RUN)$(OUT_EXT) 7 | 8 | .PHONY: clean run 9 | 10 | $(OUT): $(RUN).asm 11 | $(NASM) -o '$(RUN)$(TMP_EXT)' '$<' 12 | ld -o '$@' -s '$(RUN)$(TMP_EXT)' 13 | 14 | clean: 15 | rm -f *$(TMP_EXT) *$(OUT_EXT) 16 | 17 | run: $(OUT) 18 | ./'$<' 19 | -------------------------------------------------------------------------------- /make/templates/nasm-single.makefile: -------------------------------------------------------------------------------- 1 | .PHONY: clean run 2 | 3 | main.out: main.asm 4 | nasm -w+all -f elf64 -o main.o main.asm 5 | ld -o main.out -s main.o 6 | 7 | clean: 8 | rm -f *.o *.out 9 | 10 | run: main.out 11 | ./main.out 12 | -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | DIR ?= a/ 4 | 5 | .PHONY: all 6 | 7 | all: f g $(DIR)f 8 | cat $(DIR)f 9 | cat f 10 | cat g 11 | -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/a/Makefile: -------------------------------------------------------------------------------- 1 | ../proxy.makefile -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/a/f: -------------------------------------------------------------------------------- 1 | a 2 | -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/b/Makefile: -------------------------------------------------------------------------------- 1 | ../proxy.makefile -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/b/f: -------------------------------------------------------------------------------- 1 | b 2 | -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/f: -------------------------------------------------------------------------------- 1 | F 2 | -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/g: -------------------------------------------------------------------------------- 1 | G 2 | -------------------------------------------------------------------------------- /make/templates/toplevel_reuse/proxy.makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | .PHONY: all 3 | RULE = $(MAKE) -C ../ $@ DIR='$(notdir $(shell pwd))/' $(MAKEFLAGS) 4 | all: ; $(RULE) 5 | %: ; $(RULE) 6 | -------------------------------------------------------------------------------- /make/verbose.md: -------------------------------------------------------------------------------- 1 | # Verbose 2 | 3 | How to print the commands that are being run and other things in order to debug large makefiles. 4 | 5 | 6 | 7 | make -n -f verbose.mk 8 | make -f verbose.mk SHELL='sh -x' 9 | make -f verbose.mk -p SHELL='sh -x' 10 | -------------------------------------------------------------------------------- /make/verbose.mk: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | .PHONY: 4 | 5 | A := b 6 | 7 | a: 8 | @echo $A 9 | -------------------------------------------------------------------------------- /opencl/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /opencl/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lm -pthread -lOpenCL -lblas -lclBLAS 2 | # Alternative blas implementations with identical interfaces: 3 | # - cblas from libblas-dev 4 | # - blas from libatlas-base-dev 5 | # - openblas from libopenblas-dev 6 | -------------------------------------------------------------------------------- /opencl/benchmark-examples.md: -------------------------------------------------------------------------------- 1 | # Benchmark examples 2 | 3 | Examples under this section do benchmarks, and thus take longer to finish. 4 | 5 | This is the case of all useful examples, i.e. that actually run a real-ish application faster per dollar than the best CPU implementation we can craft, including intrinsics. 6 | -------------------------------------------------------------------------------- /opencl/clinfo.md: -------------------------------------------------------------------------------- 1 | # clinfo 2 | 3 | `glxinfo` for OpenCL: 4 | 5 | sudo clinfo 6 | 7 | Upstream: 8 | -------------------------------------------------------------------------------- /opencl/configure: -------------------------------------------------------------------------------- 1 | sudo apt-get install \ 2 | libatlas-base-dev \ 3 | libblas-dev \ 4 | libclblas-dev \ 5 | libopenblas-dev \ 6 | nvidia-opencl-dev \ 7 | ; 8 | -------------------------------------------------------------------------------- /opencl/getting-started.md: -------------------------------------------------------------------------------- 1 | # Getting started 2 | 3 | Tested in Ubuntu 15.10 NVIDIA 352, OpenCL 1.2. 4 | 5 | The day we do OpenCL 2.0, it will be put inside a subdirectory and clearly labeled. 6 | 7 | For benchmarks, make sure to use `0=3`: 8 | 9 | make O=3 10 | 11 | ## NVIDIA 12 | 13 | On Ubuntu 15.10 with an NVIDIA NVS 5400M, Lenovo T430: 14 | -------------------------------------------------------------------------------- /opencl/inc.cl: -------------------------------------------------------------------------------- 1 | __kernel void kmain(__global int *out) { 2 | out[get_global_id(0)]++; 3 | } 4 | -------------------------------------------------------------------------------- /opencl/interactive/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /opencl/interactive/Makefile_params: -------------------------------------------------------------------------------- 1 | ../Makefile_params -------------------------------------------------------------------------------- /opencl/interactive/README.md: -------------------------------------------------------------------------------- 1 | # Interactive 2 | 3 | Examples that cannot be automatically tested, or take too long. 4 | 5 | 1. [vec_io](vec_io.c) 6 | 1. [Matrix multiplication](matmul.c) 7 | -------------------------------------------------------------------------------- /opencl/interactive/common.h: -------------------------------------------------------------------------------- 1 | ../common.h -------------------------------------------------------------------------------- /opencl/interactive/matmul.cl: -------------------------------------------------------------------------------- 1 | __kernel void kmain( 2 | __global float *A, 3 | __global float *B, 4 | __global float *C, 5 | const uint n 6 | ) { 7 | uint i, j, k; 8 | float tmp; 9 | 10 | i = get_global_id(0); 11 | j = get_global_id(1); 12 | tmp = 0.0; 13 | for (k = 0; k < n; ++k) 14 | tmp += A[i*n+k] * B[k*n+j]; 15 | C[i*n+j] = tmp; 16 | } 17 | -------------------------------------------------------------------------------- /opencl/interactive/matmul_row_local.cl: -------------------------------------------------------------------------------- 1 | __kernel void kmain( 2 | __global float* A, 3 | __global float* B, 4 | __global float* C, 5 | __local float* row, 6 | const uint N 7 | ) { 8 | uint i = get_global_id(0); 9 | uint j, k; 10 | float tmp; 11 | for (k = 0; k < N; k++) 12 | row[k] = A[i*N+k]; 13 | for (j = 0; j < N; j++) { 14 | tmp = 0.0; 15 | for (k = 0; k < N; k++) 16 | tmp += row[k] * B[k*N+j]; 17 | C[i*N+j] = tmp; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /opencl/interactive/matmul_row_priv.cl: -------------------------------------------------------------------------------- 1 | __kernel void kmain( 2 | __global float* A, 3 | __global float* B, 4 | __global float* C, 5 | const uint N 6 | ) { 7 | uint i = get_global_id(0), j, k; 8 | float Arow[PRIV_ROW_SIZE], tmp; 9 | 10 | /* Cache the row to private memory. */ 11 | for (k = 0; k < N; k++) 12 | Arow[k] = A[i*N+k]; 13 | 14 | for (j = 0; j < N; j++) { 15 | tmp = 0.0; 16 | for (k = 0; k < N; k++) 17 | tmp += Arow[k] * B[k*N+j]; 18 | C[i*N+j] = tmp; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /opencl/interactive/vec_io.cl: -------------------------------------------------------------------------------- 1 | __kernel void kmain(__global float *io) { 2 | io[get_global_id(0)] += 1.0; 3 | } 4 | -------------------------------------------------------------------------------- /opencl/interactive/vec_io.vec: -------------------------------------------------------------------------------- 1 | 1 2 | 2 3 | 3 4 | -------------------------------------------------------------------------------- /opencl/interactive/vec_io_sum.cl: -------------------------------------------------------------------------------- 1 | /* Vector sum: add the first half of io, to the second half of io, 2 | * and stores it in the first half of io. 3 | * 4 | * Second half of io is untouched. 5 | * 6 | * To be less wasteful, you should set the work group size to n / 2, 7 | * but as we know, vector sum cannot be sped up by the GPU 8 | * in the first place, so it won't matter much. 9 | * */ 10 | __kernel void kmain(__global float *io) { 11 | const size_t n = get_global_size(0) / 2; 12 | const size_t i = get_global_id(0); 13 | if (i < n) { 14 | io[i] += io[i + n]; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /opencv/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /opencv/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -lm 2 | -------------------------------------------------------------------------------- /opencv/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libopencv-dev 4 | -------------------------------------------------------------------------------- /opencv/input/sky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/opencv/input/sky.jpg -------------------------------------------------------------------------------- /opengl/.gitignore: -------------------------------------------------------------------------------- 1 | /tmp*.png 2 | /tmp*.ppm 3 | -------------------------------------------------------------------------------- /opengl/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /opengl/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lm -lGL -lGLU -lglut -lglfw -lGLEW -lX11 \ 2 | -lpng \ 3 | $(shell pkg-config opencv --cflags --libs) \ 4 | -lavcodec -lswscale -lavutil \ 5 | -lv4l2 6 | -------------------------------------------------------------------------------- /opengl/about.md: -------------------------------------------------------------------------------- 1 | # About 2 | 3 | Current organization: 4 | 5 | - GLUT examples use the old immediate API, which was deprecated in 4.0 (outside of non-compatibility profile) and so you should not use it anymore. Most people consider GLFW a better alternative to GLUT. 6 | - GLFW examples use the better retained mode. Why it is better: 7 | -------------------------------------------------------------------------------- /opengl/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # Tested on Ubuntu 16.04. 3 | sudo apt-get update 4 | # Ubuntu 14.04: glfw3 not present in repos, only glfw 2. 5 | # Must compile yourself, good luck: http://stackoverflow.com/questions/17768008/how-to-build-install-glfw-3-and-use-it-in-a-linux-project 6 | sudo apt-get install \ 7 | freeglut3-dev \ 8 | libgles2-mesa-dev \ 9 | libglew-dev \ 10 | libglfw3-dev \ 11 | libopencv-dev \ 12 | libv4l-dev \ 13 | ; 14 | -------------------------------------------------------------------------------- /opengl/egl.md: -------------------------------------------------------------------------------- 1 | # EGL 2 | 3 | 4 | 5 | Looks like a Khronos standardized alternative to GLX. Possibly with focus to portability to mobile, often used with GLES. 6 | 7 | ## Desktop example 8 | 9 | TODO: haven't gotten one working yet: 10 | 11 | - Tried: 12 | - 13 | - TODO test 14 | -------------------------------------------------------------------------------- /opengl/gles/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /opengl/gles/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lGLESv2 -lglfw 2 | -------------------------------------------------------------------------------- /opengl/gles/README.md: -------------------------------------------------------------------------------- 1 | # GLES 2 | 3 | 1. [Introduction](introduction.md) 4 | 1. Examples 5 | 1. [Triangle](triangle.c) 6 | -------------------------------------------------------------------------------- /opengl/gles/introduction.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | OpenGL ES. 4 | 5 | No `glu` for it: But GLFW. 6 | 7 | Vs OpenGL: basically a subset: 8 | 9 | - 10 | - 11 | - 12 | - no immediate mode 13 | - TODO includes EGL? 14 | -------------------------------------------------------------------------------- /opengl/glfw_recursive.c.off: -------------------------------------------------------------------------------- 1 | /* 2 | Recursive calculations where the input to the next frame is the previous frame. 3 | 4 | Conway's game of life is a popular example: 5 | 6 | - http://nullprogram.com/blog/2014/06/10/ 7 | - http://gamedev.stackexchange.com/questions/87213/how-to-read-neighbor-pixels-in-glsl 8 | */ 9 | -------------------------------------------------------------------------------- /opengl/glsl.md: -------------------------------------------------------------------------------- 1 | # GLSL 2 | 3 | - 4 | 5 | Version pragma vs GL version: 6 | -------------------------------------------------------------------------------- /opengl/grass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/opengl/grass.jpg -------------------------------------------------------------------------------- /opengl/hello_world.c: -------------------------------------------------------------------------------- 1 | /* 2 | Create a window. Don't draw anything to it. 3 | */ 4 | 5 | #include 6 | 7 | #include 8 | 9 | int main(int argc, char *argv[]) { 10 | glutInit(&argc, argv); 11 | glutInitWindowSize(800, 600); 12 | glutInitWindowPosition(200, 100); 13 | glutCreateWindow(argv[0]); 14 | glutMainLoop(); 15 | return EXIT_SUCCESS; 16 | } 17 | -------------------------------------------------------------------------------- /opengl/misc.md: -------------------------------------------------------------------------------- 1 | # Misc 2 | 3 | ## float vs double 4 | 5 | - 6 | - 7 | -------------------------------------------------------------------------------- /opengl/params.makefile: -------------------------------------------------------------------------------- 1 | LIBS := -lm -pthread -lGL -lGLU -lglut 2 | -------------------------------------------------------------------------------- /opengl/performance.md: -------------------------------------------------------------------------------- 1 | # Performance 2 | 3 | ## Disable hardware rendering 4 | 5 | ## Force CPU rendering 6 | 7 | - 8 | - 9 | 10 | This is what we'd need to see that our GPU is being used. 11 | 12 | Could not do it while using the NVIDIA driver. 13 | 14 | Related: 15 | 16 | - 17 | -------------------------------------------------------------------------------- /opengl/sky.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/opengl/sky.jpg -------------------------------------------------------------------------------- /opengl/software-implementations.md: -------------------------------------------------------------------------------- 1 | # Software implementations 2 | 3 | Mesa. 4 | 5 | Non-OpenGL API renders: 6 | 7 | - , renders: files. 8 | 9 | Integrated into the Bullet Physics viewer: , and you can switch on the fly between OpenGL and tiny-renderer. 10 | 11 | TODO: OpenCL implementation?! 12 | -------------------------------------------------------------------------------- /opengl/wood.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/opengl/wood.jpg -------------------------------------------------------------------------------- /plplot/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /plplot/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lm -lplplotd 2 | -------------------------------------------------------------------------------- /plplot/README.md: -------------------------------------------------------------------------------- 1 | # PLplot 2 | 3 | Linux / Windows plotting library, with many language bindings. 4 | 5 | Plotting outputs types are called *devices*, and can either be some window on the screen or files. New ones can be added as plugins. 6 | -------------------------------------------------------------------------------- /plplot/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libplplot-dev 4 | sudo apt-get install plplot11-driver-xwin 5 | -------------------------------------------------------------------------------- /png/.gitignore: -------------------------------------------------------------------------------- 1 | *.png 2 | -------------------------------------------------------------------------------- /png/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /png/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lpng 2 | -------------------------------------------------------------------------------- /png/README.md: -------------------------------------------------------------------------------- 1 | # libpng 2 | 3 | ## Introduction 4 | 5 | 6 | 7 | 8 | 9 | Reference implementation. 10 | 11 | Manipulates PNG encoding / decoding / metadata. 12 | 13 | Part of the LSB: So basic. 14 | 15 | Library is really small, compiles in a snap. Sweet. 16 | -------------------------------------------------------------------------------- /posix/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /posix/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get install libncurses5-dev 3 | -------------------------------------------------------------------------------- /posix/curses/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /posix/curses/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lcurses 2 | -------------------------------------------------------------------------------- /posix/curses/README.md: -------------------------------------------------------------------------------- 1 | # curses 2 | 3 | 1. [ncurses](ncurses.md) 4 | 1. Examples 5 | 1. [Hello world](hello.c) 6 | 1. printw 7 | 1. [printw newline](printw_newline.c) 8 | 1. [printw twice](printw_twice.c) 9 | 1. [printw newline screen height](printw_newline_screen_height.c) 10 | 1. [clear](clear.c) 11 | 1. [Check key pressed](check_key_pressed.c) 12 | 1. Canonical. TODO. 13 | 1. [tcgetattr_dump.c](tcgetattr_dump.c) 14 | -------------------------------------------------------------------------------- /posix/curses/clear.c: -------------------------------------------------------------------------------- 1 | /* Second times overwrites the first. */ 2 | 3 | #include "common.h" 4 | #include "common_curses.h" 5 | 6 | int main(void) { 7 | initscr(); 8 | printw("before clear\n"); 9 | clear(); 10 | printw("after clear\n"); 11 | refresh(); 12 | press_any_key_to_quit(); 13 | endwin(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /posix/curses/common.h: -------------------------------------------------------------------------------- 1 | ../common.h -------------------------------------------------------------------------------- /posix/curses/common_curses.h: -------------------------------------------------------------------------------- 1 | #define ANY_KEY_TO_QUIT_MSG "press any key to quit\n" 2 | 3 | void press_any_key_to_quit() { 4 | printw(ANY_KEY_TO_QUIT_MSG); 5 | refresh(); 6 | getch(); 7 | } 8 | -------------------------------------------------------------------------------- /posix/curses/hello.c: -------------------------------------------------------------------------------- 1 | /* Clear the screen, present a message, quit when any screen is pressed. 2 | * 3 | * Previous screen is restored at the end. 4 | */ 5 | 6 | #include 7 | 8 | int main(void) { 9 | /* Startup. */ 10 | initscr(); 11 | printw("hello world. Press any key to quit."); 12 | refresh(); 13 | /* Wait for use input. */ 14 | getch(); 15 | /* Teardown. */ 16 | endwin(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /posix/curses/ncurses.md: -------------------------------------------------------------------------------- 1 | # ncurses 2 | 3 | Name of the dominant implementation. 4 | 5 | POSIX only talks about curses however. 6 | 7 | 8 | -------------------------------------------------------------------------------- /posix/curses/printw_newline.c: -------------------------------------------------------------------------------- 1 | /* Newlines do move the cursor down and to the beginning of the next line. */ 2 | 3 | #include "common.h" 4 | 5 | int main(void) { 6 | initscr(); 7 | printw("a\nb\npress any key to quit"); 8 | refresh(); 9 | getch(); 10 | endwin(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /posix/curses/printw_newline_screen_height.c: -------------------------------------------------------------------------------- 1 | /* 2 | Bad things happen: at the bottom of the screen, 3 | the cursor is imply not lowered anymore. 4 | */ 5 | 6 | #include "common.h" 7 | #include "common_curses.h" 8 | 9 | int main(void) { 10 | int i; 11 | initscr(); 12 | for (i = 0; i < 1000; ++i) { 13 | printw("%d\n", i); 14 | } 15 | press_any_key_to_quit(); 16 | endwin(); 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /posix/curses/printw_twice.c: -------------------------------------------------------------------------------- 1 | /* The current cursor position is unchanged. */ 2 | 3 | #include "common.h" 4 | #include "common_curses.h" 5 | 6 | int main(void) { 7 | initscr(); 8 | printw("first\n"); 9 | printw("second\n"); 10 | printw(ANY_KEY_TO_QUIT_MSG); 11 | refresh(); 12 | getch(); 13 | endwin(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /posix/gethostname.c: -------------------------------------------------------------------------------- 1 | /* 2 | # gethostname 3 | 4 | Copies name of current host on given string: 5 | 6 | int gethostname(char* hostname, int namelength); 7 | 8 | Name is truncated to namelength if too large 9 | */ 10 | 11 | #include "common.h" 12 | 13 | int main(void) { 14 | const int namelength = 256; 15 | char hostname[namelength]; 16 | gethostname(hostname, namelength); 17 | puts(hostname); 18 | return EXIT_SUCCESS; 19 | } 20 | -------------------------------------------------------------------------------- /posix/interactive/.gitignore: -------------------------------------------------------------------------------- 1 | /*.png 2 | -------------------------------------------------------------------------------- /posix/interactive/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /posix/interactive/benchmark/Makefile: -------------------------------------------------------------------------------- 1 | .POSIX: 2 | 3 | STD = c99 4 | 5 | .PHONY: all clean run 6 | 7 | all: main.out 8 | 9 | main.out: main.o optimize.o 10 | gcc -o $@ $^ 11 | 12 | main.o: main.c 13 | gcc -ggdb3 -O0 -c -std=$(STD) $< 14 | 15 | optimize.o: optimize.c 16 | gcc -ggdb3 -O3 -c -std=$(STD) $< 17 | 18 | run: all 19 | ulimit -Sv 500000 && ./main.out $(ARGS) 20 | 21 | clean: 22 | rm -f *.o *.out 23 | -------------------------------------------------------------------------------- /posix/interactive/benchmark/README.md: -------------------------------------------------------------------------------- 1 | # Benchmark 2 | 3 | Micro-benchmark two versions of the same function. 4 | 5 | Run as: 6 | 7 | make run ARGS='1000000 100' 8 | 9 | Requires POSIX for `getrusage`, which actually measures system time with microsecond resolution. 10 | 11 | There is no ANSI C analogue AFAIK. 12 | -------------------------------------------------------------------------------- /posix/interactive/common.h: -------------------------------------------------------------------------------- 1 | ../common.h -------------------------------------------------------------------------------- /posix/interactive/poll-loop.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | 4 | # Infinite loop test. 5 | # This is where poll shines: we manage to watch output from both pipes simultaneously, 6 | # even though they both sleep for a long time! This is perfect for webservers. 7 | 8 | sudo mknod poll0.tmp p 9 | sudo mknod poll1.tmp p 10 | sudo chmod 666 poll*.tmp 11 | ./poll.out & 12 | i=0; 13 | while true; do printf "$i"; i=$(($i+1)); sleep 1; done > poll0.tmp & 14 | j=0; 15 | while true; do printf "$j"; j=$(($j+1)); sleep 2; done > poll1.tmp & 16 | sleep 8 17 | kill %3 18 | kill %2 19 | kill %1 20 | -------------------------------------------------------------------------------- /posix/interactive/poll.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -x 3 | # Does not work with regular files. 4 | # http://stackoverflow.com/questions/11901884/how-can-select-wait-on-regular-file-descriptors-non-sockets 5 | sudo mknod poll0.tmp p 6 | sudo mknod poll1.tmp p 7 | sudo chmod 666 poll*.tmp 8 | 9 | ./poll.out & 10 | printf a > poll0.tmp 11 | printf b > poll1.tmp 12 | kill %1 13 | # => loop 14 | # => POLLIN i=0 n=1 buf=a 15 | # => loop 16 | # => POLLHUP i=0 17 | # => loop 18 | # => POLLIN i=1 n=1 buf=b 19 | # => POLLHUP i=1 20 | # => loop 21 | -------------------------------------------------------------------------------- /posix/interactive/pthread_array_sum.gnuplot: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env gnuplot 2 | set terminal png size 600, 600 3 | set style data linespoints 4 | set key autotitle columnhead 5 | set output 'pthread_array_sum.png' 6 | set title font ",16" 7 | set title "Array sum time elapsed / N threads" 8 | set xlabel "threads" 9 | set ylabel "time (s)" 10 | plot 'pthread_array_sum.dat' using 1:2 notitle 11 | -------------------------------------------------------------------------------- /posix/interactive/pthread_array_sum.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | make -B O=3 pthread_array_sum.out 4 | ./pthread_array_sum.out 1000000000 | tee pthread_array_sum.dat 5 | ./pthread_array_sum.gnuplot 6 | -------------------------------------------------------------------------------- /posix/interactive/pthread_cpu_heavy_work.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | i=1 3 | n="$(($(nproc) * 2))" 4 | rm -f pthread_cpu_heavy_work.dat 5 | while [ "$i" -le "$n" ]; do 6 | env time \ 7 | --append \ 8 | --format "$i %e %U %S" \ 9 | --output pthread_cpu_heavy_work.dat \ 10 | ./pthread_cpu_heavy_work.out 10000000000 "$i" \ 11 | ; 12 | i=$((i + 1)) 13 | done 14 | ./pthread_cpu_heavy_work.gnuplot 15 | -------------------------------------------------------------------------------- /posix/interactive/sleep_1.c: -------------------------------------------------------------------------------- 1 | /* Sleep for one second. 2 | * 3 | * Used at: 4 | * https://stackoverflow.com/questions/556405/what-do-real-user-and-sys-mean-in-the-output-of-time1/53937376#53937376 5 | */ 6 | 7 | #define _XOPEN_SOURCE 700 8 | #include 9 | #include 10 | 11 | int main(void) { 12 | sleep(1); 13 | return EXIT_SUCCESS; 14 | } 15 | -------------------------------------------------------------------------------- /posix/ls.c: -------------------------------------------------------------------------------- 1 | /* Tiny ls implementation that lists cwd. */ 2 | 3 | #include "common.h" 4 | 5 | int main(void) { 6 | DIR* dp; 7 | struct dirent* entry; 8 | 9 | dp = opendir("."); 10 | if (dp == NULL) { 11 | perror("opendir"); 12 | assert(false); 13 | } 14 | while ((entry = readdir(dp)) != NULL) { 15 | printf("%s\n", entry->d_name); 16 | } 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /posix/semaphore.md: -------------------------------------------------------------------------------- 1 | # Semaphore 2 | 3 | Semaphores are a way to synchronize threads and processes. 4 | 5 | For a very good overview, see: 6 | 7 | man sem_overview 8 | 9 | TODO: can System V do both named and unnamed semaphores? 10 | -------------------------------------------------------------------------------- /posix/socket/Makefile: -------------------------------------------------------------------------------- 1 | ../../Makefile_many -------------------------------------------------------------------------------- /posix/socket/README.md: -------------------------------------------------------------------------------- 1 | # Sockets 2 | 3 | 1. [Introduction](introduction.md) 4 | 1. Examples 5 | 1. [inet](inet/) 6 | 1. [unix](unix/) 7 | -------------------------------------------------------------------------------- /posix/socket/inet/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile -------------------------------------------------------------------------------- /posix/socket/inet/Makefile_params: -------------------------------------------------------------------------------- 1 | CFLAGS_EXTRA := -Wunused 2 | -------------------------------------------------------------------------------- /posix/socket/inet/TODO.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | 3 | - UDP. `sendto`, `recvfrom` 4 | - threaded server. TODO: is that a bad model? https://news.ycombinator.com/item?id=8343350 Apache vs Nginx 5 | - build simple sockets ncurses chat socket program. TODO: how to receive while you are writing? 6 | - poll and select. Linux also has epoll. 7 | - larger examples 8 | - P2P and server versions of: 9 | - chat 10 | - tic-tac-toe with server 11 | -------------------------------------------------------------------------------- /posix/socket/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | ./server.out & 3 | ./client.out & 4 | ./client.out & 5 | ./client.out & 6 | -------------------------------------------------------------------------------- /posix/socket/unix/Makefile: -------------------------------------------------------------------------------- 1 | ../client_server.makefile -------------------------------------------------------------------------------- /posix/socket/unix/README.md: -------------------------------------------------------------------------------- 1 | # UNIX sockets 2 | 3 | UNIX sockets, meant to be used locally on a single computer. 4 | -------------------------------------------------------------------------------- /posix/strdup.c: -------------------------------------------------------------------------------- 1 | /* 2 | # strdup 3 | 4 | malloc-copy string. 5 | */ 6 | 7 | #include "common.h" 8 | 9 | int main(void) { 10 | char *s = "abc"; 11 | char *s2; 12 | s2 = strdup(s); 13 | s2[0] = '0'; 14 | assert(strcmp(s, "abc") == 0); 15 | assert(strcmp(s2, "0bc") == 0); 16 | free(s2); 17 | return EXIT_SUCCESS; 18 | } 19 | -------------------------------------------------------------------------------- /qt/Makefile: -------------------------------------------------------------------------------- 1 | BUILD_DIR := build/ 2 | TARGET := qtcheat 3 | 4 | # Doing `qmake ../` is called a shadow build. 5 | all: 6 | if [ ! -r qt.pro ]; then qmake -project; fi && \ 7 | mkdir -p "$(BUILD_DIR)" && \ 8 | cd "$(BUILD_DIR)" && \ 9 | qmake ../ && \ 10 | make 11 | 12 | clean: 13 | rm -rf "$(BUILD_DIR)" 14 | 15 | run: all 16 | cd "$(BUILD_DIR)" && ./$(TARGET) 17 | 18 | install: all 19 | cd "$(BUILD_DIR)" && make install 20 | 21 | install-run: 22 | $(TARGET) 23 | 24 | uninstall: all 25 | cd "$(BUILD_DIR)" && make uninstall 26 | $(TARGET) 27 | -------------------------------------------------------------------------------- /qt/SlotTest.h: -------------------------------------------------------------------------------- 1 | #ifndef SLOTS_H 2 | #define SLOTS_H 3 | 4 | #include 5 | 6 | class SlotTest : public QObject 7 | { 8 | Q_OBJECT 9 | 10 | public: 11 | 12 | SlotTest(QWidget *window) : i(0), window(window) {} 13 | 14 | int i; 15 | 16 | public slots: 17 | //public Q_SLOTS: // Generally the same. 18 | 19 | void cout(); 20 | void inc(); 21 | void title(); 22 | 23 | private: 24 | 25 | QWidget *window; 26 | }; 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /qt/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libqt4-dev 4 | -------------------------------------------------------------------------------- /scons/.gitignore: -------------------------------------------------------------------------------- 1 | *.log 2 | *.o 3 | *.out 4 | .sconsign.dblite 5 | -------------------------------------------------------------------------------- /scons/add_option/README.md: -------------------------------------------------------------------------------- 1 | `AddOption` test. 2 | 3 | Short `-x` options are not supported: https://stackoverflow.com/questions/33638435/scons-with-addoption-does-not-capture-all-specified-flags/33640511#33640511 4 | 5 | The `=` seems to be mandatory. 6 | 7 | Terrible. 8 | -------------------------------------------------------------------------------- /scons/add_option/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | AddOption( 3 | '--myx', 4 | default='0', 5 | help='the help for x', 6 | ) 7 | x = GetOption('myx') 8 | env.Append(CPPDEFINES=['X=' + x]) 9 | env.Program(target='main.out', source=['main.c']) 10 | -------------------------------------------------------------------------------- /scons/add_option/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/add_option/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = '0' ] 5 | scons --myx=1 6 | [ "$(./main.out)" = '1' ] 7 | scons --myx=0 8 | [ "$(./main.out)" = '0' ] 9 | -------------------------------------------------------------------------------- /scons/alias/README.md: -------------------------------------------------------------------------------- 1 | # Alias 2 | 3 | Give a new name for a target. 4 | 5 | Both will have the same effect: 6 | 7 | scons main.out 8 | scons asdf 9 | -------------------------------------------------------------------------------- /scons/alias/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | program = env.Program(target='main.out', source=['main.c']) 3 | env.Alias('asdf', program) 4 | -------------------------------------------------------------------------------- /scons/alias/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/alias2/README.md: -------------------------------------------------------------------------------- 1 | # Alias 2 2 | 3 | `Alias` can be called multiple times with the same name to add multiple programs to an alias: 4 | 5 | scons asdf 6 | ./main.out 7 | ./main2.out 8 | -------------------------------------------------------------------------------- /scons/alias2/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | p1 = env.Program(target='main.out', source=['main.c']) 3 | p2 = env.Program(target='main2.out', source=['main2.c']) 4 | env.Alias('asdf', p1) 5 | env.Alias('asdf', p2) 6 | -------------------------------------------------------------------------------- /scons/alias2/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/alias2/main2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello2"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/arguments/README.md: -------------------------------------------------------------------------------- 1 | Command line arguments passed to SCons. 2 | -------------------------------------------------------------------------------- /scons/arguments/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | x = ARGUMENTS.get('x', '0') 3 | env.Append(CPPDEFINES=['X=' + x]) 4 | env.Program(target='main.out', source=['main.c']) 5 | -------------------------------------------------------------------------------- /scons/arguments/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/arguments/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = '0' ] 5 | scons x=1 6 | [ "$(./main.out)" = '1' ] 7 | scons x=0 8 | [ "$(./main.out)" = '0' ] 9 | -------------------------------------------------------------------------------- /scons/ccflags/README.md: -------------------------------------------------------------------------------- 1 | # CCFLAGS 2 | 3 | Pass compiler flags to C and C++. 4 | 5 | https://scons.org/doc/2.4.1/HTML/scons-user.html#app-variables 6 | 7 | Comments on type: 8 | 9 | // 10 | 11 | are only legal in C99+, so C89 should fail on those. 12 | -------------------------------------------------------------------------------- /scons/ccflags/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | std = ARGUMENTS.get('std', 'c99') 3 | env.Append(CCFLAGS='-Werror') 4 | env.Append(CCFLAGS=('-std=' + std)) 5 | env.Program(target='main.out', source=['main.c']) 6 | -------------------------------------------------------------------------------- /scons/ccflags/main.c: -------------------------------------------------------------------------------- 1 | int main(void) { 2 | // C99 3 | return 0; 4 | } 5 | -------------------------------------------------------------------------------- /scons/ccflags/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | ! scons std=c89 5 | -------------------------------------------------------------------------------- /scons/clean: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | find . \( -name '*.log' -o -name '*.o' -o -name '*.out' \) -delete 3 | -------------------------------------------------------------------------------- /scons/command/.gitignore: -------------------------------------------------------------------------------- 1 | /lib.c 2 | -------------------------------------------------------------------------------- /scons/command/README.md: -------------------------------------------------------------------------------- 1 | # Command 2 | 3 | Do an arbitrary build action. 4 | 5 | Can promise to generate a target a file, and depend on a file, so useful for source generation. 6 | -------------------------------------------------------------------------------- /scons/command/SConstruct: -------------------------------------------------------------------------------- 1 | def create_lib(target, source, env): 2 | with open(target[0].get_path(), 'w') as f: 3 | f.write('''\ 4 | #include 5 | 6 | #include "lib.h" 7 | 8 | void f() { 9 | puts("hello"); 10 | } 11 | ''') 12 | 13 | env = Environment() 14 | lib_src = env.Command( 15 | # File to generate. 16 | 'lib.c', 17 | # File dependencies. 18 | None, 19 | create_lib 20 | ) 21 | env.Program(target='main.out', source=['main.c', lib_src]) 22 | -------------------------------------------------------------------------------- /scons/command/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | void f(); 4 | #endif 5 | -------------------------------------------------------------------------------- /scons/command/main.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int main(void) { 4 | f(); 5 | } 6 | -------------------------------------------------------------------------------- /scons/command/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | 4 | scons --clean 5 | scons 6 | [ "$(./main.out)" = hello ] 7 | 8 | # Clean also removes the generated source file. 9 | scons --clean 10 | [ ! -f lib.c ] 11 | -------------------------------------------------------------------------------- /scons/default/README.md: -------------------------------------------------------------------------------- 1 | # Default 2 | 3 | cd src0 4 | 5 | scons 6 | ./main.out 7 | # => No such file or directory. 8 | ../src1/main.out 9 | # => 1 10 | 11 | scons .. 12 | ./main.out 13 | # => 0 14 | ../src1/main.out 15 | # => 1 16 | 17 | By default, SCons builds all targets under the current directory and descendants. 18 | 19 | If `Default()` is given, it change that default to the given programs. 20 | 21 | You can always go back to recurse under a given directory with `scons path/to/dir`. 22 | -------------------------------------------------------------------------------- /scons/default/src0/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | p0 = env.Program(target='main.out', source=['main.c']) 3 | p1 = env.Program(target='../src1/main.out', source=['../src1/main.c']) 4 | env.Default(p1) 5 | -------------------------------------------------------------------------------- /scons/default/src0/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("0"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/default/src1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("1"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/define/README.md: -------------------------------------------------------------------------------- 1 | Preprocessor define. 2 | -------------------------------------------------------------------------------- /scons/define/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | env.Append(CPPDEFINES=['X=1']) 3 | env.Program(target = 'main.out', source = ['main.c']) 4 | -------------------------------------------------------------------------------- /scons/define/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/define/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = '1' ] 5 | -------------------------------------------------------------------------------- /scons/glob/README.md: -------------------------------------------------------------------------------- 1 | # Multi source 2 | 3 | Use multiple C sources to build one target: 4 | -------------------------------------------------------------------------------- /scons/glob/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | env.Program(target='main.out', source=[Glob('*.c')]) 3 | -------------------------------------------------------------------------------- /scons/glob/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lib.h" 4 | 5 | void f() { 6 | puts("hello"); 7 | } 8 | -------------------------------------------------------------------------------- /scons/glob/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | void f(); 4 | #endif 5 | -------------------------------------------------------------------------------- /scons/glob/main.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int main(void) { 4 | f(); 5 | } 6 | -------------------------------------------------------------------------------- /scons/glob/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = hello ] 5 | -------------------------------------------------------------------------------- /scons/hello/README.md: -------------------------------------------------------------------------------- 1 | Hello world. 2 | -------------------------------------------------------------------------------- /scons/hello/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | env.Program(target='main.out', source=['main.c']) 3 | PathVariable('asdf', 'qwer', '', PathVariable.PathIsDir) 4 | -------------------------------------------------------------------------------- /scons/hello/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/hello/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = hello ] 5 | -------------------------------------------------------------------------------- /scons/help/README.md: -------------------------------------------------------------------------------- 1 | Hello world. 2 | -------------------------------------------------------------------------------- /scons/help/SConstruct: -------------------------------------------------------------------------------- 1 | Help('''asdf 2 | qwer''') 3 | -------------------------------------------------------------------------------- /scons/help/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import subprocess 4 | import sys 5 | 6 | out = subprocess.check_output(['scons', '-h']) 7 | assert b'asdf\nqwer' in out 8 | out = subprocess.check_output(['scons', '-H']) 9 | assert not b'asdf\nqwer' in out 10 | -------------------------------------------------------------------------------- /scons/install/README.md: -------------------------------------------------------------------------------- 1 | # Install 2 | 3 | Specify the installation directory. 4 | 5 | That directory is created if needed, and the output program is copied there. 6 | 7 | scons 8 | ./bin/main.out 9 | -------------------------------------------------------------------------------- /scons/install/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | program = env.Program(target='main.out', source=['main.c']) 3 | env.Install('bin', program) 4 | -------------------------------------------------------------------------------- /scons/install/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/library/README.md: -------------------------------------------------------------------------------- 1 | # Library 2 | 3 | Create and use shared and static libraries. 4 | -------------------------------------------------------------------------------- /scons/library/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | 3 | # Shared 4 | liba_shared = env.SharedLibrary(target='liba.so', source=['a.c']) 5 | env.Program(target='main_shared.out', source=['main.c'], LIBS=[liba_shared]) 6 | 7 | # Static 8 | liba_static = env.Library(target='a.a', source=['a.c']) 9 | env.Program(target='main_static.out', source=['main.c'], LIBS=[liba_static]) 10 | -------------------------------------------------------------------------------- /scons/library/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "a.h" 4 | 5 | void a() { 6 | puts(A_STR); 7 | } 8 | -------------------------------------------------------------------------------- /scons/library/a.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | #define A_STR "a" 4 | void a(); 5 | #endif 6 | -------------------------------------------------------------------------------- /scons/library/main.c: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | 3 | int main(void) { 4 | a(); 5 | } 6 | -------------------------------------------------------------------------------- /scons/library/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main_static.out)" = 'a' ] 5 | [ "$(LD_LIBRARY_PATH=. ./main_shared.out)" = 'a' ] 6 | # Check that it is actually dynamically linked, and we fail without it. 7 | ! env -u LD_LIBRARY_PATH ./main_shared.out 8 | -------------------------------------------------------------------------------- /scons/multi_source/README.md: -------------------------------------------------------------------------------- 1 | # Multi source 2 | 3 | Use multiple C sources to build one target. 4 | 5 | If you modify [`lib.h`](lib.h), you can observe that it does trigger rebuilds, nice! 6 | -------------------------------------------------------------------------------- /scons/multi_source/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | env.Program(target='main.out', source=['main.c', 'lib.c']) 3 | -------------------------------------------------------------------------------- /scons/multi_source/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lib.h" 4 | 5 | void f() { 6 | puts(LIB_STR); 7 | } 8 | -------------------------------------------------------------------------------- /scons/multi_source/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | #define LIB_STR "hello" 4 | void f(); 5 | #endif 6 | -------------------------------------------------------------------------------- /scons/multi_source/main.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int main(void) { 4 | f(); 5 | } 6 | -------------------------------------------------------------------------------- /scons/multi_source/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = hello ] 5 | -------------------------------------------------------------------------------- /scons/multi_target/README.md: -------------------------------------------------------------------------------- 1 | # Multi-target 2 | 3 | One `SConstruct` with multiple targets. 4 | -------------------------------------------------------------------------------- /scons/multi_target/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | env.Program(target='main.out', source=['main.c']) 3 | env.Program(target='main2.out', source=['main2.c']) 4 | env.Program(target='subdir/main.out', source=['main.c']) 5 | -------------------------------------------------------------------------------- /scons/multi_target/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/multi_target/main2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello2"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/object/README.md: -------------------------------------------------------------------------------- 1 | # Multi source 2 | 3 | https://stackoverflow.com/questions/19188494/scons-changing-compilation-flags-for-a-single-source-file 4 | -------------------------------------------------------------------------------- /scons/object/SConstruct: -------------------------------------------------------------------------------- 1 | env1 = Environment() 2 | env1.Append(CCFLAGS=('-DCLI_INT={}'.format(2))) 3 | env2 = Environment() 4 | env2.Append(CCFLAGS=('-DCLI_INT={}'.format(3))) 5 | objects = [ 6 | env1.Object('main.c'), 7 | env2.Object('lib.c'), 8 | ] 9 | env1.Program(target='main.out', source=objects) 10 | -------------------------------------------------------------------------------- /scons/object/lib.c: -------------------------------------------------------------------------------- 1 | #include "lib.h" 2 | 3 | int f() { 4 | return MY_INT + CLI_INT; 5 | } 6 | -------------------------------------------------------------------------------- /scons/object/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | #define MY_INT 1 4 | int f(); 5 | #endif 6 | -------------------------------------------------------------------------------- /scons/object/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lib.h" 4 | 5 | int main(void) { 6 | printf("%d\n", f() + CLI_INT); 7 | } 8 | -------------------------------------------------------------------------------- /scons/object/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = 6 ] 5 | -------------------------------------------------------------------------------- /scons/sconscript/README.md: -------------------------------------------------------------------------------- 1 | # SConscript 2 | 3 | `SConscript` is the name for an `scons` file called from a top-level `SConstruct`. 4 | -------------------------------------------------------------------------------- /scons/sconscript/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | topvar = 1 3 | # Make the variable visible to the child SConscript. 4 | env.Export('topvar') 5 | env.SConscript('src/SConscript') 6 | -------------------------------------------------------------------------------- /scons/sconscript/src/SConscript: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | env.Program(target='main.out', source=['main.c']) 3 | # '#" indicates the toplevel source directory. 4 | assert env.Dir('#/src').exists() 5 | # Import a variable made available with Export. 6 | env.Import('topvar') 7 | assert topvar == 1 8 | -------------------------------------------------------------------------------- /scons/sconscript/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | puts("hello"); 5 | } 6 | -------------------------------------------------------------------------------- /scons/sconscript/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | 4 | scons 5 | [ "$(./src/main.out)" = hello ] 6 | 7 | # Targets still work. 8 | scons --clean 9 | scons ./src/main.out 10 | [ "$(./src/main.out)" = hello ] 11 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/README.md: -------------------------------------------------------------------------------- 1 | # SConscript multi source 2 | 3 | A more realistic SConscript example with multiple source files in different directories. 4 | 5 | https://stackoverflow.com/questions/8810418/scons-setup-for-hierarchical-source-but-single-target/8816965#8816965 6 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/SConstruct: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | objects = [env.Object('main.c')] 3 | objects.extend(env.SConscript('lib/SConscript')) 4 | env.Program('main.out', objects) 5 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/lib/SConscript: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | objects = [env.Object('lib.c')] 3 | Return('objects') 4 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/lib/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lib.h" 4 | 5 | void f() { 6 | puts(LIB_STR); 7 | } 8 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/lib/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | #define LIB_STR "hello" 4 | void f(); 5 | #endif 6 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/main.c: -------------------------------------------------------------------------------- 1 | #include "lib/lib.h" 2 | 3 | int main(void) { 4 | f(); 5 | } 6 | -------------------------------------------------------------------------------- /scons/sconscript_multi_source/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = hello ] 5 | -------------------------------------------------------------------------------- /scons/sconscript_variant/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /scons/sconscript_variant/README.md: -------------------------------------------------------------------------------- 1 | # SConscript variant 2 | 3 | We can specify a variant to a single `SConscript`. 4 | 5 | Source and object files only for that `SConscript` are put into the given variant dir. 6 | -------------------------------------------------------------------------------- /scons/sconscript_variant/SConstruct: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | env = Environment() 4 | objects = [env.Object('main.c')] 5 | build_dir = 'build' 6 | objects.extend(env.SConscript( 7 | os.path.join('lib', 'SConscript'), 8 | variant_dir=os.path.join(build_dir, 'lib'), 9 | )) 10 | objects.extend(env.SConscript( 11 | os.path.join('lib2', 'SConscript'), 12 | variant_dir=os.path.join(build_dir, 'lib2'), 13 | )) 14 | env.Program('main.out', objects) 15 | -------------------------------------------------------------------------------- /scons/sconscript_variant/lib/SConscript: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | objects = [env.Object('lib.c')] 3 | Return('objects') 4 | -------------------------------------------------------------------------------- /scons/sconscript_variant/lib/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lib.h" 4 | 5 | void f() { 6 | puts(LIB_STR); 7 | } 8 | -------------------------------------------------------------------------------- /scons/sconscript_variant/lib/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB_H 2 | #define LIB_H 3 | #define LIB_STR "hello" 4 | void f(); 5 | #endif 6 | -------------------------------------------------------------------------------- /scons/sconscript_variant/lib2/SConscript: -------------------------------------------------------------------------------- 1 | env = Environment() 2 | objects = [env.Object('lib.c')] 3 | Return('objects') 4 | -------------------------------------------------------------------------------- /scons/sconscript_variant/lib2/lib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "lib.h" 4 | 5 | void f2() { 6 | puts(LIB2_STR); 7 | } 8 | -------------------------------------------------------------------------------- /scons/sconscript_variant/lib2/lib.h: -------------------------------------------------------------------------------- 1 | #ifndef LIB2_H 2 | #define LIB2_H 3 | #define LIB2_STR "hello2" 4 | void f2(); 5 | #endif 6 | -------------------------------------------------------------------------------- /scons/sconscript_variant/main.c: -------------------------------------------------------------------------------- 1 | #include "lib/lib.h" 2 | #include "lib2/lib.h" 3 | 4 | int main(void) { 5 | f(); 6 | f2(); 7 | } 8 | -------------------------------------------------------------------------------- /scons/sconscript_variant/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./main.out)" = "$(printf 'hello\nhello2')" ] 5 | -------------------------------------------------------------------------------- /scons/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | top="$(pwd)" 4 | log_path="${top}/test.log" 5 | ./clean 6 | ls -d */ | parallel "$@" --will-cite " 7 | cd '${top}/{}' 8 | if [ -e test ]; then 9 | echo '{}' 10 | ret=0 11 | ./test >> '$log_path' 2>&1 || ret=$? 12 | if [ $? -ne 0 ]; then 13 | echo 'Test failed: {}' 14 | fi 15 | fi 16 | " 17 | echo 'All tests passed' 18 | -------------------------------------------------------------------------------- /scons/two_envs/README.md: -------------------------------------------------------------------------------- 1 | # Two envs 2 | 3 | This is how you can setup different command line options per executable: https://stackoverflow.com/questions/19188494/scons-changing-compilation-flags-for-a-single-source-file 4 | -------------------------------------------------------------------------------- /scons/two_envs/SConstruct: -------------------------------------------------------------------------------- 1 | import os.path 2 | 3 | def mkenv(x): 4 | env = Environment() 5 | env.Append(CPPDEFINES=['X=' + x]) 6 | variant_dir = 'build-{}'.format(x) 7 | env.VariantDir(variant_dir, 'src', duplicate=0) 8 | program = env.Program( 9 | target=os.path.join(variant_dir, 'main.out'), 10 | source=[os.path.join(variant_dir, 'main.c')] 11 | ) 12 | 13 | mkenv('0') 14 | mkenv('1') 15 | -------------------------------------------------------------------------------- /scons/two_envs/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/two_envs/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons 4 | [ "$(./build-0/main.out)" = 0 ] 5 | [ "$(./build-1/main.out)" = 1 ] 6 | -------------------------------------------------------------------------------- /scons/variables/README.md: -------------------------------------------------------------------------------- 1 | # Variables 2 | 3 | How to use `Variables` for setting command line variables and `Help`. 4 | 5 | Try out help with: 6 | 7 | scons -h 8 | scons -H 9 | -------------------------------------------------------------------------------- /scons/variables/SConstruct: -------------------------------------------------------------------------------- 1 | variables = Variables(None, ARGUMENTS) 2 | variables.Add('X', 'Value of X', 1) 3 | variables.Add('Y', 'Value of Y', 2) 4 | env = Environment(variables=variables) 5 | env.Append(CPPDEFINES={ 6 | 'X': '${X}', 7 | 'Y': '${Y}', 8 | }) 9 | env.Program( 10 | source=['main.c'], 11 | target='main.out', 12 | ) 13 | Help(variables.GenerateHelpText(env)) 14 | -------------------------------------------------------------------------------- /scons/variables/main.c: -------------------------------------------------------------------------------- 1 | int main(void) { 2 | return X + Y; 3 | } 4 | -------------------------------------------------------------------------------- /scons/variables/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons --clean 4 | scons 5 | ./main.out 6 | [ $? = 3 ] 7 | scons --clean 8 | scons X=3 Y=4 9 | ./main.out 10 | [ $? = 7 ] 11 | -------------------------------------------------------------------------------- /scons/variant_dir/README.md: -------------------------------------------------------------------------------- 1 | # VariantDir 2 | 3 | Create multiple versions of the build depending on config files or command line parameters. 4 | 5 | We could do something more advanced by: 6 | 7 | - hashing all input arguments like `x` and name the output directory after them 8 | - cat the input arguments to a file in that directory to enable identifying the directories 9 | 10 | Can also be used for out-of-tree builds: https://stackoverflow.com/questions/3720142/how-to-force-scons-output-exe-obj-lib-dll-to-specific-build-directory 11 | -------------------------------------------------------------------------------- /scons/variant_dir/SConstruct: -------------------------------------------------------------------------------- 1 | import os.path 2 | env = Environment() 3 | x = ARGUMENTS.get('x', '0') 4 | env.Append(CPPDEFINES=['X=' + x]) 5 | variant_dir = 'build_{}'.format(x) 6 | # duplicate: https://scons.org/doc/2.4.1/HTML/scons-user.html#idp1378838508 7 | # If on, by default uses hardlinks. Can be turned into softlinks with --duplicate=soft-copy. 8 | env.VariantDir(variant_dir, 'src', duplicate=1) 9 | program1 = env.Program( 10 | target=os.path.join(variant_dir, 'main.out'), 11 | source=[os.path.join(variant_dir, 'main.c')] 12 | ) 13 | -------------------------------------------------------------------------------- /scons/variant_dir/build_0/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/variant_dir/build_1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/variant_dir/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | printf("%d\n", X); 5 | } 6 | -------------------------------------------------------------------------------- /scons/variant_dir/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | scons --clean 4 | scons 5 | [ "$(./build_0/main.out)" = 0 ] 6 | [ ! -d build_1 ] 7 | scons x=1 8 | [ "$(./build_1/main.out)" = 1 ] 9 | -------------------------------------------------------------------------------- /sdl/FreeSans.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/sdl/FreeSans.ttf -------------------------------------------------------------------------------- /sdl/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /sdl/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lSDL2 -lSDL2_image -lSDL2_ttf \ 2 | -lm -lGL -lGLEW 3 | -------------------------------------------------------------------------------- /sdl/TODO.md: -------------------------------------------------------------------------------- 1 | # TODO 2 | 3 | - take screenshots 4 | -------------------------------------------------------------------------------- /sdl/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install \ 4 | libsdl2-dev \ 5 | libsdl2-image-dev \ 6 | libsdl2-ttf-dev \ 7 | ; 8 | -------------------------------------------------------------------------------- /sdl/flower.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/sdl/flower.png -------------------------------------------------------------------------------- /sdl/users.md: -------------------------------------------------------------------------------- 1 | # Users 2 | 3 | - 4 | -------------------------------------------------------------------------------- /shared_library/abi/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/2171177/what-is-an-application-binary-interface-abi/54967743#54967743 2 | -------------------------------------------------------------------------------- /shared_library/abi/clean: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | rm -rf v1/*.o v1/*.so v2/*.o v2/*.so *.out 3 | -------------------------------------------------------------------------------- /shared_library/abi/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "mylib.h" 5 | 6 | int main(void) { 7 | mylib_mystruct *myobject = mylib_init(1); 8 | assert(myobject->old_field == 1); 9 | free(myobject); 10 | return EXIT_SUCCESS; 11 | } 12 | -------------------------------------------------------------------------------- /shared_library/abi/test: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eu 3 | cc='gcc -pedantic-errors -std=c89 -Wall -Wextra' 4 | for d in v1 v2_bad v2_good; do 5 | $cc -fPIC -c -o "$d/mylib.o" "$d/mylib.c" 6 | $cc -L . -shared -o "$d/libmylib.so" "$d/mylib.o" 7 | done 8 | $cc -I v1 -L v1 -o main.out main.c -lmylib 9 | LD_LIBRARY_PATH=v1 ./main.out 10 | ! LD_LIBRARY_PATH=v2_bad ./main.out 11 | LD_LIBRARY_PATH=v2_good ./main.out 12 | $cc -I v2_bad -L v2_bad -o main.out main.c -lmylib 13 | LD_LIBRARY_PATH=v2_bad ./main.out 14 | -------------------------------------------------------------------------------- /shared_library/abi/v1/mylib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mylib.h" 4 | 5 | mylib_mystruct* mylib_init(int old_field) { 6 | mylib_mystruct *myobject; 7 | myobject = malloc(sizeof(mylib_mystruct)); 8 | myobject->old_field = old_field; 9 | return myobject; 10 | } 11 | -------------------------------------------------------------------------------- /shared_library/abi/v1/mylib.h: -------------------------------------------------------------------------------- 1 | #ifndef MYLIB_H 2 | #define MYLIB_H 3 | 4 | typedef struct { 5 | int old_field; 6 | } mylib_mystruct; 7 | 8 | mylib_mystruct* mylib_init(int old_field); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /shared_library/abi/v2_bad/mylib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mylib.h" 4 | 5 | mylib_mystruct* mylib_init(int old_field) { 6 | mylib_mystruct *myobject; 7 | myobject = malloc(sizeof(mylib_mystruct)); 8 | myobject->old_field = old_field; 9 | myobject->new_field = 2; 10 | return myobject; 11 | } 12 | -------------------------------------------------------------------------------- /shared_library/abi/v2_bad/mylib.h: -------------------------------------------------------------------------------- 1 | #ifndef MYLIB_H 2 | #define MYLIB_H 3 | 4 | typedef struct { 5 | int new_field; 6 | int old_field; 7 | } mylib_mystruct; 8 | 9 | mylib_mystruct* mylib_init(int old_field); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /shared_library/abi/v2_good/mylib.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "mylib.h" 4 | 5 | mylib_mystruct* mylib_init(int old_field) { 6 | mylib_mystruct *myobject; 7 | myobject = malloc(sizeof(mylib_mystruct)); 8 | myobject->old_field = old_field; 9 | myobject->new_field = 2; 10 | return myobject; 11 | } 12 | -------------------------------------------------------------------------------- /shared_library/abi/v2_good/mylib.h: -------------------------------------------------------------------------------- 1 | #ifndef MYLIB_H 2 | #define MYLIB_H 3 | 4 | typedef struct { 5 | int old_field; 6 | int new_field; 7 | } mylib_mystruct; 8 | 9 | mylib_mystruct* mylib_init(int old_field); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /shared_library/basic/README.md: -------------------------------------------------------------------------------- 1 | # Basic 2 | 3 | Basic example. Creates a shared and a static library from `a.c` and `b.c`. 4 | 5 | Used in the following answers: 6 | 7 | - https://stackoverflow.com/questions/3961446/why-does-gcc-not-implicitly-supply-the-fpic-flag-when-compiling-static-librarie/65005457#65005457 8 | - https://unix.stackexchange.com/questions/89211/how-to-test-whether-a-linux-binary-was-compiled-as-position-independent-code/435038#435038 9 | - https://unix.stackexchange.com/questions/226524/what-system-call-is-used-to-load-libraries-in-linux/462710#462710 10 | -------------------------------------------------------------------------------- /shared_library/basic/a.c: -------------------------------------------------------------------------------- 1 | /* Not mandatory in this example since we define the function here. 2 | * 3 | * But still a good idea to ensure that the prototypes are compatible. 4 | * 5 | * Often required because of header struct declarations. 6 | */ 7 | #include "a.h" 8 | 9 | int a(void) { return 1; } 10 | -------------------------------------------------------------------------------- /shared_library/basic/a.h: -------------------------------------------------------------------------------- 1 | #ifndef A_H 2 | #define A_H 3 | 4 | int a(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /shared_library/basic/b.c: -------------------------------------------------------------------------------- 1 | #include "b.h" 2 | 3 | int b(void) { return 2; } 4 | -------------------------------------------------------------------------------- /shared_library/basic/b.h: -------------------------------------------------------------------------------- 1 | #ifndef B_H 2 | #define B_H 3 | 4 | int b(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /shared_library/basic/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "a.h" 5 | #include "b.h" 6 | 7 | int main(void) { 8 | assert(a() == 1); 9 | assert(b() == 2); 10 | return EXIT_SUCCESS; 11 | } 12 | -------------------------------------------------------------------------------- /shared_library/lib_lib_dependency/README.md: -------------------------------------------------------------------------------- 1 | # Lib-lib dependency 2 | 3 | `libcirosantilli_a.so` depends on `libcirosantilli_b.so`, check that with `readelf -d | grep NEEDED`. 4 | -------------------------------------------------------------------------------- /shared_library/lib_lib_dependency/a.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "a.h" 4 | #include "b.h" 5 | 6 | int a(void) { 7 | return b() + 1; 8 | } 9 | -------------------------------------------------------------------------------- /shared_library/lib_lib_dependency/a.h: -------------------------------------------------------------------------------- 1 | #ifndef A_H 2 | #define A_H 3 | 4 | int a(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /shared_library/lib_lib_dependency/b.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "b.h" 4 | 5 | int b(void) { return 2; } 6 | -------------------------------------------------------------------------------- /shared_library/lib_lib_dependency/b.h: -------------------------------------------------------------------------------- 1 | #ifndef B_H 2 | #define B_H 3 | 4 | int b(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /shared_library/lib_lib_dependency/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "a.h" 5 | 6 | int main(void) { 7 | assert(a() == 3); 8 | return EXIT_SUCCESS; 9 | } 10 | -------------------------------------------------------------------------------- /shared_library/symbol_version/README.md: -------------------------------------------------------------------------------- 1 | https://stackoverflow.com/questions/8823267/linking-against-older-symbol-version-in-a-so-file/51856826#51856826 2 | -------------------------------------------------------------------------------- /shared_library/symbol_version/a.c: -------------------------------------------------------------------------------- 1 | #include "a.h" 2 | 3 | __asm__(".symver a1,a@LIBA_1"); 4 | int a1(void) { 5 | return 1; 6 | } 7 | 8 | /* @@ means "default version". */ 9 | __asm__(".symver a2,a@@LIBA_2"); 10 | int a2(void) { 11 | return 2; 12 | } 13 | -------------------------------------------------------------------------------- /shared_library/symbol_version/a.h: -------------------------------------------------------------------------------- 1 | #ifndef A_H 2 | #define A_H 3 | 4 | int a(void); 5 | 6 | #endif 7 | -------------------------------------------------------------------------------- /shared_library/symbol_version/a.map: -------------------------------------------------------------------------------- 1 | LIBA_1{ 2 | global: 3 | a; 4 | local: 5 | *; 6 | }; 7 | 8 | LIBA_2{ 9 | global: 10 | a; 11 | local: 12 | *; 13 | }; 14 | -------------------------------------------------------------------------------- /shared_library/symbol_version/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "a.h" 5 | 6 | /* TODO: any clearer way of doing this maybe 7 | * with some compilation option only? 8 | */ 9 | #if defined(V1) 10 | __asm__(".symver a,a@LIBA_1"); 11 | #elif defined(V2) 12 | __asm__(".symver a,a@LIBA_2"); 13 | #endif 14 | 15 | int main(void) { 16 | #if defined(V1) 17 | assert(a() == 1); 18 | #else 19 | assert(a() == 2); 20 | #endif 21 | return EXIT_SUCCESS; 22 | } 23 | -------------------------------------------------------------------------------- /swig/.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | *.pyc 3 | *.so 4 | build/ 5 | example.py 6 | example.java 7 | exampleJNI.java 8 | example_wrap_*.c 9 | -------------------------------------------------------------------------------- /swig/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | static { 3 | /* 4 | Looks for file "${java.library.path}/libexample_java.so". 5 | `example` class is loaded from that file. 6 | */ 7 | System.loadLibrary("example_java"); 8 | } 9 | public static void main(String argv[]) { 10 | System.out.println(example.fact(4)); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /swig/README.md: -------------------------------------------------------------------------------- 1 | # SWIG 2 | 3 | Tested on Ubuntu 15.10, SWIG 2.0.12, Python 2.7, Java OpenJDK 8. 4 | 5 | 1. [Introduction](introduction.md) 6 | 1. [Example](example.md) 7 | -------------------------------------------------------------------------------- /swig/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libpython2.7-dev openjdk-8-jdk swig 4 | echo 'MANUAL STEP: set and export your JAVA_HOME' 5 | -------------------------------------------------------------------------------- /swig/example.c: -------------------------------------------------------------------------------- 1 | #include "example.h" 2 | 3 | int fact(int n) { 4 | if (n < 0){ /* This should probably return an error, but this is simpler */ 5 | return 0; 6 | } 7 | if (n == 0) { 8 | return 1; 9 | } 10 | else { 11 | /* testing for overflow would be a good idea here */ 12 | return n * fact(n-1); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /swig/example.h: -------------------------------------------------------------------------------- 1 | int fact(int n); 2 | -------------------------------------------------------------------------------- /swig/example.i: -------------------------------------------------------------------------------- 1 | %module example 2 | 3 | %{ 4 | #define SWIG_FILE_WITH_INIT 5 | #include "example.h" 6 | %} 7 | 8 | int fact(int n); 9 | -------------------------------------------------------------------------------- /swig/example.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | We are going to generate and use a Python and Java API from a single annotated C source code. Note how DRY this is. 4 | 5 | The best place to find simple examples like this are the language specific sections of the documentation: 6 | 7 | If something fails, first get a minimal example of C native interface on the respective language to ensure you have all setup well. 8 | -------------------------------------------------------------------------------- /swig/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import example 3 | print(example.fact(4)) 4 | -------------------------------------------------------------------------------- /swig/setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import distutils.core 4 | 5 | example_module = distutils.core.Extension( 6 | # Generates a _example.so file. 7 | # TODO: can this be changed? I'd like to use an `example_python` file to be more explicit. 8 | '_example', 9 | sources=['example_wrap_py.c', 'example.c'], 10 | ) 11 | distutils.core.setup( 12 | name = 'example', 13 | version = '0.1', 14 | author = "SWIG Docs", 15 | description = """Simple swig example from docs""", 16 | ext_modules = [example_module], 17 | py_modules = ["example"], 18 | ) 19 | -------------------------------------------------------------------------------- /template/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /template/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lXXX 2 | -------------------------------------------------------------------------------- /template/README.md: -------------------------------------------------------------------------------- 1 | # XXX 2 | 3 | Template to try out a new C library. 4 | -------------------------------------------------------------------------------- /template/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | sudo apt-get install libXXX-dev 4 | -------------------------------------------------------------------------------- /template/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | 6 | int main (void) { 7 | return EXIT_SUCCESS; 8 | } 9 | -------------------------------------------------------------------------------- /v4l2/.gitignore: -------------------------------------------------------------------------------- 1 | *.ppm 2 | *.jpg 3 | -------------------------------------------------------------------------------- /v4l2/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /v4l2/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lv4l2 \ 2 | -lX11 -lGL -lGLU 3 | ALL_DEPEND := common_v4l2.h 4 | -------------------------------------------------------------------------------- /v4l2/README.md: -------------------------------------------------------------------------------- 1 | # v4l2 2 | 3 | 1. [Introduction](introduciton.md) 4 | 1. [v4l-utils](v4l-utils.md) 5 | 1. Examples 6 | 1. Official examples 7 | 1. [take_pictures_docs.c](take_pictures_docs.c) 8 | 1. [take_pictures_contrib.c](take_pictures_contrib.c) 9 | 1. [window_show_opengl.c](window_show_opengl.c) 10 | 1. Minimized official examples 11 | 1. [take_pictures_docs_min.c](take_pictures_docs_min.c) 12 | 1. [window_show_opengl_min.c](window_show_opengl_min.c) 13 | -------------------------------------------------------------------------------- /v4l2/bibliography.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cirosantilli/cpp-cheat/d18a11865ac105507d036f8f12a457ad9686a664/v4l2/bibliography.md -------------------------------------------------------------------------------- /v4l2/clean: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | rm -f *.ppm 3 | -------------------------------------------------------------------------------- /v4l2/configure: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | sudo apt-get update 3 | # TODO: get rid of libopencv. At least use something more minimal like ffmpeg. 4 | sudo apt-get install libv4l-dev 5 | ../opengl/configure 6 | -------------------------------------------------------------------------------- /v4l2/introduction.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | Practical getting started: 4 | 5 | 6 | 7 | Docs: Generated from their rst docs inside the Linux kernel tree. 8 | 9 | Git: 10 | 11 | Examples in source tree: `contrib/test` 12 | 13 | Can be used in Android apparently: 14 | -------------------------------------------------------------------------------- /v4l2/v4l-utils.md: -------------------------------------------------------------------------------- 1 | # v4l-utils 2 | 3 | CLI utilities. 4 | 5 | ## v4l2-ctl 6 | 7 | List all video devices: 8 | 9 | v4l2-ctl --list-devices 10 | 11 | 12 | -------------------------------------------------------------------------------- /x11/Makefile: -------------------------------------------------------------------------------- 1 | ../Makefile_many -------------------------------------------------------------------------------- /x11/Makefile_params: -------------------------------------------------------------------------------- 1 | LIBS := -lX11 2 | -------------------------------------------------------------------------------- /x11/README.md: -------------------------------------------------------------------------------- 1 | # X11 2 | 3 | 1. [Hello world](hello_world.c) 4 | 5 | Major graphical system on Linux systems. 6 | 7 | You should use something like GTK or Qt to be more portable to Windows and Mac. 8 | 9 | ## xlib 10 | 11 | 12 | -------------------------------------------------------------------------------- /x11/bibliography.md: -------------------------------------------------------------------------------- 1 | # Bibliography 2 | 3 | - 4 | 5 | Nice hello world and intro. 6 | --------------------------------------------------------------------------------