Skip to content

Instantly share code, notes, and snippets.

@domiyanyue
domiyanyue / forward_declare.cpp
Created April 25, 2020 19:14
forward declar.cpp
int bar();
int foo(){
return bar();
}
int bar(){
return 1;
}
@domiyanyue
domiyanyue / compile.txt
Created April 25, 2020 21:23
compile example -c
g++ -c example.cpp -o example.o
@domiyanyue
domiyanyue / compile.txt
Created April 25, 2020 21:44
Compile multiple source files
g++ -c main.cpp -o main.o
g++ -c util.cpp -o util.o
g++ main.o util.o -o a.out
@domiyanyue
domiyanyue / example.cpp
Created April 25, 2020 21:49
multiple declaration
int foo();
int foo();
int foo(){
return 1;
}
int main(){
return foo();
}
@domiyanyue
domiyanyue / compile.txt
Created April 25, 2020 21:58
error1 ODR
one_definition_rule# g++ -c error1.cpp
error1.cpp: In function ‘int foo()’:
error1.cpp:5:5: error: redefinition of ‘int foo()’
int foo(){
^~~
error1.cpp:1:5: note: ‘int foo()’ previously defined here
int foo() {
^~~
@domiyanyue
domiyanyue / compile.txt
Created April 25, 2020 22:03
error2 ODR
muti_file# g++ -c main.cpp
muti_file# g++ -c main.cpp -o main.o
muti_file# g++ -c util.cpp -o util.o
muti_file# g++ main.o util.o -o a.out
util.o: In function `foo()':
util.cpp:(.text+0x0): multiple definition of `foo()'
main.o:main.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
@domiyanyue
domiyanyue / 1_main.cpp
Created April 26, 2020 19:21
no_header_example_source_files
int foo();
int main(){
return foo();
}
@domiyanyue
domiyanyue / compile.sh
Created April 26, 2020 19:22
no_header_example_compile
no_header_file# g++ -c main.cpp -o main.o
no_header_file# g++ -c util.cpp -o util.o
no_header_file# g++ main.o util.o -o a.out
@domiyanyue
domiyanyue / 1_main.cpp
Last active April 27, 2020 01:07
header file example use it
#include "util.h"
int main(){
return foo();
}
@domiyanyue
domiyanyue / util.h
Created April 27, 2020 02:16
example struct in header 1
struct Time {
int hour;
int minute;
Time(int h, int m) : hour(h), minute(m){}
};
bool isDayTime(Time t);