如何驗證gcc正常使用,編譯c以及運行
過程
要驗證GCC(GNU Compiler Collection)是否正常使用,您可以按照以下步驟進行操作:
-
檢查GCC是否安裝:
打開終端或命令行界面,輸入以下命令來檢查GCC是否已安裝:
gcc --version
如果GCC已正確安裝,您將看到GCC的版本信息。如果沒有安裝,您會看到類似“command not found”的錯誤信息。
2. 編寫一個簡單的C程序:
創建一個名爲hello.c
的文件,並輸入以下代碼:
#include <stdio.h> int main() { printf("Hello, World!\n"); return 0; }
保存並關閉文件。
3. 編譯C程序:
在終端或命令行界面中,導航到包含hello.c
文件的目錄,並輸入以下命令來編譯它:
gcc hello.c -o hello
此命令將使用GCC編譯器將hello.c
文件編譯爲一個名爲hello
的可執行文件。
4. 運行編譯後的程序:
輸入以下命令來運行編譯後的程序:
./hello
如果一切正常,您將在終端或命令行界面中看到“Hello, World!”的輸出。
通過以上步驟,您可以驗證GCC是否正常工作。如果在編譯或運行程序時遇到錯誤,請檢查GCC的安裝和配置,以及確保您的C代碼沒有語法錯誤。
過程執行,編譯c以及演示
[[email protected] ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello.c [root@vm-qa-mcw.xx.xx.com ares]# which gcc /usr/local/bin/gcc [root@vm-qa-mcw.xx.xx.com ares]# cat hello.c #include <stdio.h> int main() { printf("Hello, World!\n"); return 0; } [root@vm-qa-mcw.xx.xx.com ares]# [root@vm-qa-mcw.xx.xx.com ares]# [root@vm-qa-mcw.xx.xx.com ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello.c [root@vm-qa-mcw.xx.xx.com ares]# which gcc /usr/local/bin/gcc [root@vm-qa-mcw.xx.xx.com ares]# gcc --version gcc (GCC) 7.5.0 Copyright (C) 2017 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. [root@vm-qa-mcw.xx.xx.com ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello.c [root@vm-qa-mcw.xx.xx.com ares]# gcc hello.c -o hello [root@vm-qa-mcw.xx.xx.com ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello hello.c [root@vm-qa-mcw.xx.xx.com ares]# ./hello Hello, World! [root@vm-qa-mcw.xx.xx.com ares]#
c調用shell腳本
在C++中調用Shell腳本,你可以使用system()
函數,它位於<cstdlib>
庫中。這個函數可以執行一個shell命令,並等待它完成。下面是一個簡單的例子:
#include <cstdlib> // for system() int main() { int result = system("./your_script.sh"); if (result == 0) { std::cout << "Shell script executed successfully." << std::endl; } else { std::cout << "Shell script execution failed." << std::endl; } return 0; }
在這個例子中,system("./your_script.sh")
會執行名爲your_script.sh
的Shell腳本。system()
函數會返回命令的退出狀態,通常,如果命令成功執行,返回0;否則返回非零值。
但是,需要注意的是,system()
函數有一些缺點。例如,它可能會引入安全漏洞(特別是當你執行的命令包含用戶輸入時),並且它無法捕獲命令的標準輸出或錯誤輸出。如果你需要更精細的控制,你可能需要使用更復雜的庫,如popen()
或fork()
和exec()
的組合。
另外,system()
函數在不同的操作系統和平臺上可能會有不同的行爲,因此如果你的代碼需要在多個平臺上運行,你需要特別小心。
最後,別忘了給你的Shell腳本添加執行權限。在Linux或Mac OS上,你可以使用chmod +x your_script.sh
命令來添加執行權限。
上面執行報錯了
# gcc mcw.c -o mcw mcw.c:1:10: fatal error: cstdlib: No such file or directory #include <cstdlib> // for system() ^~~~~~~~~ compilation terminated.
下面用這個
在C語言中,調用Shell腳本通常涉及使用system()
函數,這個函數定義在<stdlib.h>
頭文件中。system()
函數會啓動一個子shell來執行指定的命令,並等待命令執行完成。下面是一個簡單的例子:
#include <stdlib.h> // for system() #include <stdio.h> // for printf() int main() { int result = system("./your_script.sh"); if (result == 0) { printf("Shell script executed successfully.\n"); } else { printf("Shell script execution failed.\n"); } return 0; }
在這個例子中,system("./your_script.sh")
會調用名爲your_script.sh
的Shell腳本。system()
函數返回命令的退出狀態。如果命令成功執行,通常返回0;如果執行失敗,返回非零值。
在使用system()
函數時,請確保以下幾點:
-
Shell腳本的路徑是正確的,並且腳本具有執行權限。在Unix-like系統(如Linux或macOS)上,你可以使用
chmod +x your_script.sh
來添加執行權限。 -
如果Shell腳本依賴於特定的環境變量或當前工作目錄,你需要確保這些在調用腳本之前都已經正確設置。
-
system()
函數會啓動一個新的shell進程來執行命令,這可能會帶來性能開銷。如果你需要頻繁地調用Shell命令,或者需要更精細地控制輸入/輸出,你可能需要考慮使用更低級別的進程控制函數,如fork()
和exec()
系列函數。 -
使用
system()
時要特別小心安全問題,尤其是當命令字符串包含用戶輸入時。不正確的處理可能導致命令注入攻擊。確保不要直接拼接用戶輸入到命令字符串中,或者使用安全的方法來轉義或過濾用戶輸入。 -
system()
函數的行爲可能依賴於特定的操作系統和shell環境,因此在跨平臺開發中需要格外小心。
最後,記住在程序結束時檢查system()
的返回值,以確定Shell腳本是否成功執行。這有助於你調試程序,並在必要時向用戶報告錯誤。
[root@mcwtest ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello hello.c mcw.c your_script.sh [root@mcwtest ares]# cat mcw.c #include <stdlib.h> // for system() #include <stdio.h> // for printf() int main() { int result = system("./your_script.sh"); if (result == 0) { printf("Shell script executed successfully.\n"); } else { printf("Shell script execution failed.\n"); } return 0; } [root@mcwtest ares]# cat your_script.sh echo "wo shi machangwei" [root@mcwtest ares]# gcc mcw.c -o mcw [root@mcwtest ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello hello.c mcw mcw.c your_script.sh [root@mcwtest ares]# mcw bash: mcw: command not found [root@mcwtest ares]# ./mcw sh: ./your_script.sh: Permission denied Shell script execution failed. [root@mcwtest ares]# ls -lh your_script.sh -rw-r--r-- 1 root root 26 Apr 24 12:42 your_script.sh [root@mcwtest ares]# chmod u+x your_script.sh [root@mcwtest ares]# ./mcw wo shi machangwei Shell script executed successfully. [root@mcwtest ares]#
這樣應該也可以調用其他腳本,只要是終端命令都可以。也可以其他程序調用c程序命令,這相當於程序命令,相當於終端命令了。裏面應該也可以調用python
如果再研究下c怎麼傳遞參數,然後將參數傳給裏面調用的腳本。這樣就可以將我們的程序不暴露出來了,多了一層保護。比如zabbix,也不一定要調用python,shell採集機器的監控數據,也可以調用c腳本。
objdump,將上面gcc編譯後的二進制文件,顯示出彙編情況
objdump
是一個在 Linux 和其他類 Unix 系統上常用的程序,用於顯示二進制文件的信息。它可以用來查看各種格式的目標文件的信息,如可執行文件、目標文件、共享庫文件等。使用 objdump
,你可以查看二進制文件的彙編代碼、符號表、重定位表以及其他相關信息。
以下是使用 objdump
顯示二進制文件信息的一些基本用法和示例:
基本用法
objdump
的基本語法是:
objdump [options] file |
其中 file
是你要分析的二進制文件的路徑。
查看彙編代碼
如果你想查看二進制文件的彙編代碼,可以使用 -d
或 --disassemble
選項。例如:
objdump -d binaryfile |
這個命令會顯示 binaryfile
的反彙編結果,包括地址、機器碼和彙編指令等信息。
查看符號表信息
使用 -t
或 --syms
選項,你可以查看二進制文件的符號表信息。例如:
objdump -t binaryfile |
這個命令會顯示 binaryfile
的符號表,包括符號名、地址、大小和類型等信息。
其他常用選項
-f
或--file-headers
:顯示文件的整體頭部摘要信息。-h
或--section-headers
:顯示目標文件中各個段的頭部摘要信息。-I
或--info
:顯示支持的目標文件格式和 CPU 架構。-j name
或--section=name
:顯示指定段的信息。-m machine
或--architecture=machine
:指定反彙編目標文件時使用的架構。
這些選項可以幫助你更深入地瞭解二進制文件的結構和內容。通過組合這些選項,你可以定製 objdump
的輸出以滿足你的具體需求。
請注意,爲了獲得最佳的結果,你可能需要對彙編語言和你正在分析的二進制文件的上下文有一定的瞭解。此外,雖然 objdump
是一個強大的工具,但它可能無法提供關於二進制文件的所有信息,特別是當文件被加密、混淆或壓縮時。
[root@mcwtest ares]# objdump -t mcw mcw: file format elf64-x86-64 SYMBOL TABLE: 0000000000400238 l d .interp 0000000000000000 .interp 0000000000400254 l d .note.ABI-tag 0000000000000000 .note.ABI-tag 0000000000400278 l d .hash 0000000000000000 .hash 00000000004002a0 l d .dynsym 0000000000000000 .dynsym 0000000000400318 l d .dynstr 0000000000000000 .dynstr 000000000040035c l d .gnu.version 0000000000000000 .gnu.version 0000000000400368 l d .gnu.version_r 0000000000000000 .gnu.version_r 0000000000400388 l d .rela.dyn 0000000000000000 .rela.dyn 00000000004003a0 l d .rela.plt 0000000000000000 .rela.plt 0000000000400400 l d .init 0000000000000000 .init 0000000000400420 l d .plt 0000000000000000 .plt 0000000000400470 l d .text 0000000000000000 .text 00000000004005f4 l d .fini 0000000000000000 .fini 0000000000400600 l d .rodata 0000000000000000 .rodata 0000000000400668 l d .eh_frame_hdr 0000000000000000 .eh_frame_hdr 00000000004006a0 l d .eh_frame 0000000000000000 .eh_frame 0000000000600e18 l d .init_array 0000000000000000 .init_array 0000000000600e20 l d .fini_array 0000000000000000 .fini_array 0000000000600e28 l d .dynamic 0000000000000000 .dynamic 0000000000600ff8 l d .got 0000000000000000 .got 0000000000601000 l d .got.plt 0000000000000000 .got.plt 0000000000601038 l d .data 0000000000000000 .data 0000000000601048 l d .bss 0000000000000000 .bss 0000000000000000 l d .comment 0000000000000000 .comment 0000000000000000 l df *ABS* 0000000000000000 crtstuff.c 00000000004004a0 l F .text 0000000000000000 deregister_tm_clones 00000000004004d0 l F .text 0000000000000000 register_tm_clones 0000000000400510 l F .text 0000000000000000 __do_global_dtors_aux 0000000000601048 l O .bss 0000000000000001 completed.6942 0000000000600e20 l O .fini_array 0000000000000000 __do_global_dtors_aux_fini_array_entry 0000000000400540 l F .text 0000000000000000 frame_dummy 0000000000600e18 l O .init_array 0000000000000000 __frame_dummy_init_array_entry 0000000000000000 l df *ABS* 0000000000000000 mcw.c 0000000000000000 l df *ABS* 0000000000000000 crtstuff.c 0000000000400790 l O .eh_frame 0000000000000000 __FRAME_END__ 0000000000000000 l df *ABS* 0000000000000000 0000000000600e20 l .init_array 0000000000000000 __init_array_end 0000000000600e28 l O .dynamic 0000000000000000 _DYNAMIC 0000000000600e18 l .init_array 0000000000000000 __init_array_start 0000000000400668 l .eh_frame_hdr 0000000000000000 __GNU_EH_FRAME_HDR 0000000000601000 l O .got.plt 0000000000000000 _GLOBAL_OFFSET_TABLE_ 00000000004005f0 g F .text 0000000000000002 __libc_csu_fini 0000000000601038 w .data 0000000000000000 data_start 0000000000000000 F *UND* 0000000000000000 puts@@GLIBC_2.2.5 0000000000601048 g .data 0000000000000000 _edata 00000000004005f4 g F .fini 0000000000000000 _fini 0000000000000000 F *UND* 0000000000000000 system@@GLIBC_2.2.5 0000000000000000 F *UND* 0000000000000000 __libc_start_main@@GLIBC_2.2.5 0000000000601038 g .data 0000000000000000 __data_start 0000000000000000 w *UND* 0000000000000000 __gmon_start__ 0000000000601040 g O .data 0000000000000000 .hidden __dso_handle 0000000000400600 g O .rodata 0000000000000004 _IO_stdin_used 0000000000400580 g F .text 0000000000000065 __libc_csu_init 0000000000601050 g .bss 0000000000000000 _end 0000000000400470 g F .text 0000000000000000 _start 0000000000601048 g .bss 0000000000000000 __bss_start 0000000000400547 g F .text 0000000000000038 main 0000000000601048 g O .data 0000000000000000 .hidden __TMC_END__ 0000000000400400 g F .init 0000000000000000 _init [root@mcwtest ares]# ls gcc-7.5.0 gcc-7.5.0-build gcc-7.5.0.tar.gz gccbak hello hello.c mcw mcw.c your_script.sh [root@mcwtest ares]# objdump -d mcw mcw: file format elf64-x86-64 Disassembly of section .init: 0000000000400400 <_init>: 400400: 48 83 ec 08 sub $0x8,%rsp 400404: 48 8b 05 ed 0b 20 00 mov 0x200bed(%rip),%rax # 600ff8 <__gmon_start__> 40040b: 48 85 c0 test %rax,%rax 40040e: 74 05 je 400415 <_init+0x15> 400410: e8 4b 00 00 00 callq 400460 <__gmon_start__@plt> 400415: 48 83 c4 08 add $0x8,%rsp 400419: c3 retq Disassembly of section .plt: 0000000000400420 <.plt>: 400420: ff 35 e2 0b 20 00 pushq 0x200be2(%rip) # 601008 <_GLOBAL_OFFSET_TABLE_+0x8> 400426: ff 25 e4 0b 20 00 jmpq *0x200be4(%rip) # 601010 <_GLOBAL_OFFSET_TABLE_+0x10> 40042c: 0f 1f 40 00 nopl 0x0(%rax) 0000000000400430 <puts@plt>: 400430: ff 25 e2 0b 20 00 jmpq *0x200be2(%rip) # 601018 <puts@GLIBC_2.2.5> 400436: 68 00 00 00 00 pushq $0x0 40043b: e9 e0 ff ff ff jmpq 400420 <.plt> 0000000000400440 <system@plt>: 400440: ff 25 da 0b 20 00 jmpq *0x200bda(%rip) # 601020 <system@GLIBC_2.2.5> 400446: 68 01 00 00 00 pushq $0x1 40044b: e9 d0 ff ff ff jmpq 400420 <.plt> 0000000000400450 <__libc_start_main@plt>: 400450: ff 25 d2 0b 20 00 jmpq *0x200bd2(%rip) # 601028 <__libc_start_main@GLIBC_2.2.5> 400456: 68 02 00 00 00 pushq $0x2 40045b: e9 c0 ff ff ff jmpq 400420 <.plt> 0000000000400460 <__gmon_start__@plt>: 400460: ff 25 ca 0b 20 00 jmpq *0x200bca(%rip) # 601030 <__gmon_start__> 400466: 68 03 00 00 00 pushq $0x3 40046b: e9 b0 ff ff ff jmpq 400420 <.plt> Disassembly of section .text: 0000000000400470 <_start>: 400470: 31 ed xor %ebp,%ebp 400472: 49 89 d1 mov %rdx,%r9 400475: 5e pop %rsi 400476: 48 89 e2 mov %rsp,%rdx 400479: 48 83 e4 f0 and $0xfffffffffffffff0,%rsp 40047d: 50 push %rax 40047e: 54 push %rsp 40047f: 49 c7 c0 f0 05 40 00 mov $0x4005f0,%r8 400486: 48 c7 c1 80 05 40 00 mov $0x400580,%rcx 40048d: 48 c7 c7 47 05 40 00 mov $0x400547,%rdi 400494: e8 b7 ff ff ff callq 400450 <__libc_start_main@plt> 400499: f4 hlt 40049a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 00000000004004a0 <deregister_tm_clones>: 4004a0: 55 push %rbp 4004a1: b8 48 10 60 00 mov $0x601048,%eax 4004a6: 48 3d 48 10 60 00 cmp $0x601048,%rax 4004ac: 48 89 e5 mov %rsp,%rbp 4004af: 74 17 je 4004c8 <deregister_tm_clones+0x28> 4004b1: b8 00 00 00 00 mov $0x0,%eax 4004b6: 48 85 c0 test %rax,%rax 4004b9: 74 0d je 4004c8 <deregister_tm_clones+0x28> 4004bb: 5d pop %rbp 4004bc: bf 48 10 60 00 mov $0x601048,%edi 4004c1: ff e0 jmpq *%rax 4004c3: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 4004c8: 5d pop %rbp 4004c9: c3 retq 4004ca: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 00000000004004d0 <register_tm_clones>: 4004d0: be 48 10 60 00 mov $0x601048,%esi 4004d5: 55 push %rbp 4004d6: 48 81 ee 48 10 60 00 sub $0x601048,%rsi 4004dd: 48 89 e5 mov %rsp,%rbp 4004e0: 48 c1 fe 03 sar $0x3,%rsi 4004e4: 48 89 f0 mov %rsi,%rax 4004e7: 48 c1 e8 3f shr $0x3f,%rax 4004eb: 48 01 c6 add %rax,%rsi 4004ee: 48 d1 fe sar %rsi 4004f1: 74 15 je 400508 <register_tm_clones+0x38> 4004f3: b8 00 00 00 00 mov $0x0,%eax 4004f8: 48 85 c0 test %rax,%rax 4004fb: 74 0b je 400508 <register_tm_clones+0x38> 4004fd: 5d pop %rbp 4004fe: bf 48 10 60 00 mov $0x601048,%edi 400503: ff e0 jmpq *%rax 400505: 0f 1f 00 nopl (%rax) 400508: 5d pop %rbp 400509: c3 retq 40050a: 66 0f 1f 44 00 00 nopw 0x0(%rax,%rax,1) 0000000000400510 <__do_global_dtors_aux>: 400510: 80 3d 31 0b 20 00 00 cmpb $0x0,0x200b31(%rip) # 601048 <__TMC_END__> 400517: 75 17 jne 400530 <__do_global_dtors_aux+0x20> 400519: 55 push %rbp 40051a: 48 89 e5 mov %rsp,%rbp 40051d: e8 7e ff ff ff callq 4004a0 <deregister_tm_clones> 400522: c6 05 1f 0b 20 00 01 movb $0x1,0x200b1f(%rip) # 601048 <__TMC_END__> 400529: 5d pop %rbp 40052a: c3 retq 40052b: 0f 1f 44 00 00 nopl 0x0(%rax,%rax,1) 400530: f3 c3 repz retq 400532: 0f 1f 40 00 nopl 0x0(%rax) 400536: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 40053d: 00 00 00 0000000000400540 <frame_dummy>: 400540: 55 push %rbp 400541: 48 89 e5 mov %rsp,%rbp 400544: 5d pop %rbp 400545: eb 89 jmp 4004d0 <register_tm_clones> 0000000000400547 <main>: 400547: 55 push %rbp 400548: 48 89 e5 mov %rsp,%rbp 40054b: 48 83 ec 10 sub $0x10,%rsp 40054f: bf 08 06 40 00 mov $0x400608,%edi 400554: e8 e7 fe ff ff callq 400440 <system@plt> 400559: 89 45 fc mov %eax,-0x4(%rbp) 40055c: 83 7d fc 00 cmpl $0x0,-0x4(%rbp) 400560: 75 0c jne 40056e <main+0x27> 400562: bf 20 06 40 00 mov $0x400620,%edi 400567: e8 c4 fe ff ff callq 400430 <puts@plt> 40056c: eb 0a jmp 400578 <main+0x31> 40056e: bf 48 06 40 00 mov $0x400648,%edi 400573: e8 b8 fe ff ff callq 400430 <puts@plt> 400578: b8 00 00 00 00 mov $0x0,%eax 40057d: c9 leaveq 40057e: c3 retq 40057f: 90 nop 0000000000400580 <__libc_csu_init>: 400580: 41 57 push %r15 400582: 41 89 ff mov %edi,%r15d 400585: 41 56 push %r14 400587: 49 89 f6 mov %rsi,%r14 40058a: 41 55 push %r13 40058c: 49 89 d5 mov %rdx,%r13 40058f: 41 54 push %r12 400591: 4c 8d 25 80 08 20 00 lea 0x200880(%rip),%r12 # 600e18 <__frame_dummy_init_array_entry> 400598: 55 push %rbp 400599: 48 8d 2d 80 08 20 00 lea 0x200880(%rip),%rbp # 600e20 <__init_array_end> 4005a0: 53 push %rbx 4005a1: 4c 29 e5 sub %r12,%rbp 4005a4: 31 db xor %ebx,%ebx 4005a6: 48 c1 fd 03 sar $0x3,%rbp 4005aa: 48 83 ec 08 sub $0x8,%rsp 4005ae: e8 4d fe ff ff callq 400400 <_init> 4005b3: 48 85 ed test %rbp,%rbp 4005b6: 74 1e je 4005d6 <__libc_csu_init+0x56> 4005b8: 0f 1f 84 00 00 00 00 nopl 0x0(%rax,%rax,1) 4005bf: 00 4005c0: 4c 89 ea mov %r13,%rdx 4005c3: 4c 89 f6 mov %r14,%rsi 4005c6: 44 89 ff mov %r15d,%edi 4005c9: 41 ff 14 dc callq *(%r12,%rbx,8) 4005cd: 48 83 c3 01 add $0x1,%rbx 4005d1: 48 39 eb cmp %rbp,%rbx 4005d4: 75 ea jne 4005c0 <__libc_csu_init+0x40> 4005d6: 48 83 c4 08 add $0x8,%rsp 4005da: 5b pop %rbx 4005db: 5d pop %rbp 4005dc: 41 5c pop %r12 4005de: 41 5d pop %r13 4005e0: 41 5e pop %r14 4005e2: 41 5f pop %r15 4005e4: c3 retq 4005e5: 90 nop 4005e6: 66 2e 0f 1f 84 00 00 nopw %cs:0x0(%rax,%rax,1) 4005ed: 00 00 00 00000000004005f0 <__libc_csu_fini>: 4005f0: f3 c3 repz retq Disassembly of section .fini: 00000000004005f4 <_fini>: 4005f4: 48 83 ec 08 sub $0x8,%rsp 4005f8: 48 83 c4 08 add $0x8,%rsp 4005fc: c3 retq [root@mcwtest ares]#