1 C2CPP
Published:
Makefile
CC = g++
CXX = g++
CXXFLAGS = -g -Wall -std=c++20
CC: compiler for C source filesCXX: compiler for C++-std=c++20
hello.cpp
#include <iostream> // cpp headers don't have .h
int main() {
std::cout << "hello world c" << 2 << "cpp" << std::endl;
}
cout: console output C++ has namespace. In C, if two library have the same function names, linker can’t link
intandchar *are mixed- Left shift
<<redefined forchar *(can’t redefine forint)
Target
target: source
- Tab: recipe
CC = g++
CXX = g++
If CC = gcc, gcc will invoke linker ld with the C standard libraries, problematic!!!
- Or specify
ld hello.o libstdc++...in the recipe line
hello: hello.o
g++ hello.o -o hello
hello: hello.cpp
g++ -g -Wall -std=c++20 -c hello.c -o hello.o
.PHONY: clean
clean:
rm *.o hello
.PHONY: all
all: clean hello
Namespace
#include <iostream> // cpp headers don't have .h
int main() {
using namespace std;
cout << "hello world c" << 2 << "cpp" << endl;
}
Define your own namespace:
#include <iostream>
namespace c2cpp {
int main() {
using namespace std;
cout << "hello c" << 2 << "cpp!" << endl;
return 0;
}
} // namespace c2cpp
int main() { // global namespace
return c2cpp::main();
}
String
In standard library: string
string s;
s = "hello";
s = s + "world"
- Can’t concatenate by
s + t s = s + 100won’t work.operator+()undefineds += 100works.operator+=()overloaded
