linux下的C 程式設計

2021-06-21 00:19:32 字數 3301 閱讀 3485

第一步,要安裝c++的編譯器g++

使用如下命令:

root@wl-ms-7673:/home/wl/桌面/c++# apt-get install g++

第二步,開始我們的hello world

使用vim建立helloworld.cpp,輸入如下:

root@wl-ms-7673:/home/wl/桌面/c++# cat helloworld.cpp 

#include int main()

root@wl-ms-7673:/home/wl/桌面/c++# cat -n test1.hpp

1 #include 2 int calculate(int const& a, int const& b);

root@wl-ms-7673:/home/wl/桌面/c++# cat -n test.c

cat: test.c: 沒有那個檔案或目錄

root@wl-ms-7673:/home/wl/桌面/c++# cat -n test.cpp

1 #include "test.hpp"

2 using namespace std;

3 int main()

4 {5 int x(3);

6 int y(5);

7 int c;

8 c = calculate(x,y);

9 cout<2 #include "test1.hpp"

root@wl-ms-7673:/home/wl/桌面/c++#

然後進行編譯:

root@wl-ms-7673:/home/wl/桌面/c++# vim test1.hpp

root@wl-ms-7673:/home/wl/桌面/c++# vim test.cpp

root@wl-ms-7673:/home/wl/桌面/c++# vim test.hpp

root@wl-ms-7673:/home/wl/桌面/c++# g++ -c test1.cpp -o test1.o

root@wl-ms-7673:/home/wl/桌面/c++# g++ -c test.cpp -o test.o

root@wl-ms-7673:/home/wl/桌面/c++# g++ test1.o test.o -o out

root@wl-ms-7673:/home/wl/桌面/c++# ls

a.out helloworld helloworld.cpp out test1.cpp test1.hpp test1.o test.cpp test.hpp test.o

root@wl-ms-7673:/home/wl/桌面/c++# ./out

8

這樣,我們有多個檔案的話編譯效率很低,下面試試makefile的強大威力!

我們建立makefile檔案如下:

root@wl-ms-7673:/home/wl/桌面/c++# cat -n makefile 

1 mycplusplus : test.o test1.o

2 g++ test1.o test.o -o mycplusplus

3 test1.o : test1.cpp

4 g++ -c test1.cpp -o test1.o

5 test.o : test.cpp

6 g++ -c test.cpp -o test.o

7 root@wl-ms-7673:/home/wl/桌面/c++# ls -al

總用量 32

drwxr-xr-x 2 root root 4096 3月 16 15:55 .

drwxr-xr-x 9 wl wl 4096 3月 16 15:16 ..

-rw-r--r-- 1 root root 96 3月 16 15:19 helloworld.cpp

-rw-r--r-- 1 root root 159 3月 16 15:54 makefile

-rw-r--r-- 1 root root 99 3月 16 15:34 test1.cpp

-rw-r--r-- 1 root root 63 3月 16 15:35 test1.hpp

-rw-r--r-- 1 root root 123 3月 16 15:37 test.cpp

-rw-r--r-- 1 root root 41 3月 16 15:37 test.hpp

root@wl-ms-7673:/home/wl/桌面/c++# make

g++ -c test.cpp -o test.o

g++ -c test1.cpp -o test1.o

g++ test1.o test.o -o mycplusplus

root@wl-ms-7673:/home/wl/桌面/c++# ls -al

總用量 48

drwxr-xr-x 2 root root 4096 3月 16 15:55 .

drwxr-xr-x 9 wl wl 4096 3月 16 15:16 ..

-rw-r--r-- 1 root root 96 3月 16 15:19 helloworld.cpp

-rw-r--r-- 1 root root 159 3月 16 15:54 makefile

-rwxr-xr-x 1 root root 7850 3月 16 15:55 mycplusplus

-rw-r--r-- 1 root root 99 3月 16 15:34 test1.cpp

-rw-r--r-- 1 root root 63 3月 16 15:35 test1.hpp

-rw-r--r-- 1 root root 1556 3月 16 15:55 test1.o

-rw-r--r-- 1 root root 123 3月 16 15:37 test.cpp

-rw-r--r-- 1 root root 41 3月 16 15:37 test.hpp

-rw-r--r-- 1 root root 1824 3月 16 15:55 test.o

root@wl-ms-7673:/home/wl/桌面/c++# ./mycplusplus

8root@wl-ms-7673:/home/wl/桌面/c++#

執行makefile後自動進行編譯並輸出mycplusplus

執行./mycplusplus輸出結果~

大功告成,很easy吧~

Linux下的C程式設計

1.命名習慣 define pi 3.1415926 int min value,max value void send data void 變數名,函式名全都小寫,單詞之間用 連線。2.case範圍 gnu c支援case x.y這樣的語法,區間 x,y 的數都會滿足這個case的條件,例如 sw...

Linux下的C程式設計

linux下的c程式設計 學習內容 源程式編譯 makefile的編寫 程式庫的鏈結 程式的除錯 標頭檔案和系統求助 1.源程式的編譯 在linux下面,如果要編譯乙個c語言源程式,我們要使用gnu的gcc編譯器.下面我們以乙個例項來說明如何使用gcc編譯器.假設我們有下面乙個非常簡單的源程式 he...

linux下c 程式設計

yum install gcc c 安裝c 環境 然後開始編寫c 程式,假設放到test1.cpp中,g o test11 test1.cpp就會編譯test1.cpp生成test11檔案。test11就可以執行該程式 查詢乙個包是否被安裝 rpm q rpm package name 列出所有被安...