c++ compile
$表示系统提示符
// unique_ptr::reset example #include <iostream> #include <memory> int main () { std::unique_ptr<int> up; // empty up.reset (new int); // takes ownership of pointer *up=5; std::cout << *up << '\n'; up.reset (new int); // deletes managed object, acquires new pointer,可以自动释放先前的指针,并重置为当前的指针 *up=10; std::cout << *up << '\n'; up.reset(); // deletes managed object return 0; }
muhe221@muhe:~/share/work/test$ g++ -c xx.cpp
xx.cpp: In function ‘int main()’:
xx.cpp:6:3: error: ‘unique_ptr’ is not a member of ‘std’
xx.cpp:6:19: error: expected primary-expression before ‘int’
xx.cpp:6:19: error: expected ‘;’ before ‘int’
xx.cpp:8:3: error: ‘up’ was not declared in this scope
muhe221@muhe:~/share/work/test$ g++ -std=c++11 xx.cpp
cc1plus: error: unrecognized command line option ‘-std=c++11’
出现这个编译错误的原因在g++ gcc 版本不够高。
添加源(Ubuntu)
$sudo add-apt-repository ppa:ubuntu-toolchain-r/test
$ sudo apt-get update
安装4.8版本
$ sudo apt-get install gcc-4.8
muhe221@muhe:~/code/m-rel_shep_mtk6795$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
但是我们可以找对应的参数即可-std=c++0x,所以就不用安装gcc-4.8
muhe221@muhe:~/share/work/test$ g++ -std=c++0x xx.cpp //c++0x是一套标准,这里gcc 版本为4.6.3 不同gcc版本编译命令可能不一样
muhe221@muhe:~/share/work/test$ ./a.out
5
10
--------------------------------------------------------------------------------------
make file
//test.h #include <iostream> #ifndef TEST_H #define TEST_H using namespace std; class Test { public: Test() { cout << "Test()" << endl; } /* static void* operator new(size_t size) throw() { cout << "operator new() size:" << size << endl; return NULL; } */ void print(); ~Test() { cout << "~Test()" << endl; } }; #endif
//test.cpp #include "test.h" void Test::print() { cout << "cout to output log" << endl; clog << "clog to output log" << endl; cerr << "cerr to output log" << endl; }
//Haha.cpp #include "test.h" #include "Sales_item.h" #include <memory> unique_ptr<Test> getTest() { unique_ptr<Test> test(new Test); return test; } static void print() { Test* t = new Test(); t->print(); delete t; } int main() { print(); printf("\n"); unique_ptr<Test> test = getTest(); test->print(); sleep(10); return 1; }
#Makefile Haha: test.o Haha.o g++ test.o Haha.o -o Haha test.o: test.cpp test.h g++ -c test.cpp Haha.o: Haha.cpp g++ -c -std=c++0x Haha.cpp clean: rm -fr test.o Haha.o Haha
muhe221@muhe:~/share/work/cpp$ make clean
rm -fr test.o Haha.o Haha
muhe221@muhe:~/share/work/cpp$ make
g++ -c test.cpp
g++ -c -std=c++0x Haha.cpp
g++ test.o Haha.o -o Haha
muhe221@muhe:~/share/work/cpp$ ./Haha
Test()
cout to output log
clog to output log
cerr to output log
~Test()
Test()
cout to output log
clog to output log
cerr to output log
//10 s 后输出
~Test()
----------------------------------------------
#prepare files
CPP_SRCS:=$(shell find ./$(SRC) -name *.cpp)
这里又调用了一次shell,执行了find。这次是查找.cpp文件,然后把文件列表保存在CPP_SRCS里面
找到的文件是带着相对的路径名的,这个很有用。
OBJS:=$(CPP_SRCS:%.cpp=$(FinalOutput)%.o)
这里是替换,$(CPP_SRCS:%.cpp=$(FinalOutput)%.o)
的意思就是说,把CPP_SRCS里面,每个cpp部分结尾的字符串,.cpp部分都替换成.o,并且在前面加上$(FinalOutput)字符串

浙公网安备 33010602011771号