① 怎麼在linux中編譯與運行c++的原代碼*.cpp文件
不是用make,是用g++,比如,你的文件名是test.cpp,所在的路徑是/home/test/test.cpp
你就在終端里輸入:cd
/home/test
然後輸入g++
-o
test
test.cpp
就已經生成編譯好的文件了。
如果想看運行結果可以在終端輸入
./test
(還是在剛才的路徑下輸入)
② linux下編譯程序源碼,執行make命令時報錯如圖,有沒有什麼頭緒或者建議,謝謝大神了
linux下所有軟體源碼包的安裝方式一般都會在readme中有詳細的官方說明,對於gerbv如下圖所示
上述大部分內容對linux下所有軟體包的安裝都適用。
③ 如何編譯一個linux下的驅動模塊
Linux內核源碼路徑:/usr/src/linux(這個源碼是從kernel.org網站download的2.4.18版本)
按照《linux設備驅動開發詳解》一書中的步驟實現經典例子"hello,world!"的例子。
具體步驟如下:
=============================================
1.源碼如下:
/*
* hello.c -- the example of printf "hello world!" in the screen of driver program
*/
#include <linux/init.h>
#include <linux/mole.h>
MODULE_LICENSE("Dual BSD/GPL");/* declare the license of the mole ,it is necessary */
static int hello_init(void)
{
printk(KERN_ALERT "Hello World enter!\n");
return 0;
}
static int hello_exit(void)
{
printk(KERN_ALERT "Hello world exit!\n");
}
mole_init(hello_init); /* load the mole */
mole_exit(hello_exit); /* unload the mole */
進入目錄:
[root@Alex_linux /]#cd /work/jiakun_test/moletest
[root@Alex_linux moletest]# vi hello.c
然後拷入上面書上的源碼。
2.編譯代碼:
1>.首先我在2.4內核的虛擬機上進行編譯,編譯過程如下:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
其中-I選項指定內河源碼,也就是內核源碼樹路徑。編譯結果:
hello.c:1:22: net/sock.h: No such file or directory
hello.c: In function `hello_init':
hello.c:6: warning: implicit declaration of function `printk'
hello.c:6: `KERN_ALERT' undeclared (first use in this function)
hello.c:6: (Each undeclared identifier is reported only once
hello.c:6: for each function it appears in.)
hello.c:6: parse error before string constant
hello.c: In function `hello_exit':
hello.c:11: `KERN_ALERT' undeclared (first use in this function)
hello.c:11: parse error before string constant
hello.c: At top level:
hello.c:13: warning: type defaults to `int' in declaration of `mole_init'
hello.c:13: warning: parameter names (without types) in function declaration
hello.c:13: warning: data definition has no type or storage class
hello.c:14: warning: type defaults to `int' in declaration of `mole_exit'
hello.c:14: warning: parameter names (without types) in function declaration
hello.c:14: warning: data definition has no type or storage class
在網上查詢有網友提示沒有引入kernel.h
解決:vi hello.c
在第一行加入:#include <linux/kernel.h>
再次編譯仍然報KERN_ALERT沒有聲明
修改編譯條件-I,再次編譯:
[root@Alex_linux moletest]#gcc -D__KERNEL__ -I /usr/src/linux -DMODULE -Wall -O2 -c -o hello.o hello.c
[root@Alex_linux moletest]#ls
hello.c hello.o Makefile
[root@Alex_linux moletest]#
2>.接著我嘗試在2.6內核的虛擬機上進行編譯
編譯過程如下:
[root@JiaKun moletest]# ls
hello.c makefile
[root@JiaKun moletest]# vi hello.c
[root@JiaKun moletest]# make
make -C /mylinux/kernel/2.4.18-rmk7 M=/home/alex/test/moletest moles
make: *** /mylinux/kernel/2.4.18-rmk7: No such file or directory. Stop.
make: *** [moles] Error 2
[root@JiaKun moletest]# vi makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
scripts/Makefile.build:17: /home/alex/test/moletest/Makefile: No such file or directory
make[2]: *** No rule to make target `/home/alex/test/moletest/Makefile'. Stop.
make[1]: *** [_mole_/home/alex/test/moletest] Error 2
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
make: *** [moles] Error 2
[root@JiaKun moletest]# mv makefile Makefile
[root@JiaKun moletest]# make
make -C /usr/src/kernels/2.6.18-53.el5-i686 M=/home/alex/test/moletest moles
make[1]: Entering directory `/usr/src/kernels/2.6.18-53.el5-i686'
CC [M] /home/alex/test/moletest/hello.o
Building moles, stage 2.
MODPOST
CC /home/alex/test/moletest/hello.mod.o
LD [M] /home/alex/test/moletest/hello.ko
make[1]: Leaving directory `/usr/src/kernels/2.6.18-53.el5-i686'
[root@JiaKun moletest]# ls
hello.c hello.ko hello.mod.c hello.mod.o hello.o Makefile Mole.symvers
3.執行代碼,載入驅動模塊:
2.4內核載入模塊:
insmod ./hello.o
但是此時並沒有輸出printk列印的信息。但是可以在/var/log/messages 中看到列印的信息,這是由於KERN_ALERT優先順序不夠高。這里
需要修改為:KERN_EMERG。再次編譯,載入模塊即可以看到結果
2.6內核載入模塊:
[root@JiaKun moletest]# insmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:44 2008 ...
JiaKun kernel: Hello, world
有的朋友可能會出現insmod命令找不到的錯誤,這可能有下面幾個原因:
<1> 你的系統沒有安裝mole-init-tools工具,關於此問題,只需安裝即可,但是一般裝完系統是有這個命令的。
<2> 環境變數沒有添加導致不能使用該命令。使用echo $PATH即可查看PATH環境變數,發現沒有/sbin這個路徑,所以你當然不能使用insmod這個命令了。解決的方法很簡單,只需在命令行輸入:
PATH = "$PATH:/sbin"即可添加。(insmod在/sbin這個目錄下,你可以使用whereis insmod查看)。
<3> insmod這個命令需要在root許可權下才能使用。
載入完成後你可以輸入lsmod查看hello這個模塊哦。
4.卸載驅動模塊:rmmod hello.
載入模塊後就可在屏幕上看到如下信息:Hello world enter.
卸載時就可在屏幕上看到如下信息:hello world exit.
[root@JiaKun moletest]# rmmod hello.ko
[root@JiaKun moletest]#
Message from syslogd@ at Sat Jul 26 19:52:58 2008 ...
JiaKun kernel: Goodbye, cruel world
另外,如果有多個文件,則按下列方式編寫Makefile文件(file1.c、file2.c):
obj -m := molename.o
mole-objs := file1.o file2.o
轉載僅供參考,版權屬於原作者。祝你愉快,滿意請採納哦
④ 如何編譯linux源代碼
首先uname -r看一下你當前的linux內核版本
1、linux的源碼是在/usr/src這個目錄下,此目錄有你電腦上各個版本的linux內核源代碼,用uname -r命令可以查看你當前使用的是哪套內核,你把你下載的內核源碼也保存到這個目錄之下。
2、配置內核 make menuconfig,根據你的需要來進行選擇,設置完保存之後會在當前目錄下生成.config配置文件,以後的編譯會根據這個來有選擇的編譯。
3、編譯,依次執行make、make bzImage、make moles、make moles
4、安裝,make install
5、.創建系統啟動映像,到 /boot 目錄下,執行 mkinitramfs -o initrd.img-2.6.36 2.6.36
6、修改啟動項,因為你在啟動的時候會出現多個內核供你選擇,此事要選擇你剛編譯的那個版本,如果你的電腦沒有等待時間,就會進入默認的,默認的那個取決於 /boot/grub/grub.cfg 文件的設置,找到if [ "${linux_gfx_mode}" != "text" ]這行,他的第一個就是你默認啟動的那個內核,如果你剛編譯的內核是在下面,就把代表這個內核的幾行代碼移到第一位如:
menuentry 'Ubuntu, with Linux 3.2.0-35-generic' --class ubuntu --class gnu-linux --class gnu --class os {
recordfail
gfxmode $linux_gfx_mode
insmod gzio
insmod part_msdos
insmod ext2
set root='(hd0,msdos1)'
search --no-floppy --fs-uuid --set=root 9961c170-2566-41ac-8155-18f231c1bea5
linux/boot/vmlinuz-3.2.0-35-generic root=UUID=9961c170-2566-41ac-8155-18f231c1bea5 ro quiet splash $vt_handoff
initrd/boot/initrd.img-3.2.0-35-generic
}
當然你也可以修改 set default="0"來決定用哪個,看看你的內核在第幾位,default就填幾,不過我用過這種方法,貌似不好用。
重啟過後你編譯的內核源碼就成功地運行了,如果出現問題,比如滑鼠不能用,usb不識別等問題就好好查查你的make menuconfig這一步,改好後就萬事ok了。
最後再用uname -r看看你的linux內核版本。是不是你剛下的那個呢!有沒有成就感?
⑤ linux怎樣編譯git源碼包
方法/步驟1
用git --version命令檢查是否已經安裝
2
在CentOS5的版本,由於yum源中沒有git,所以需要預先安裝一系列的依賴包。在CentOS6的yum源中已經有git的版本了,可以直接使用yum源進行安裝。
3
yum -y install git
但是yum源中安裝的git版本是1.7.1,Github等需要的Git版本最低都不能低於1.7.2 。所以我們一般不用上面的方法,而是下載git源碼編譯安裝。
END
編譯安裝git
1
首先更新系統
yum -y update
更新完成之後有6.5變成6.7了
⑥ linux終端下如何進行C語言編譯
1、首先在linux下判斷是否安裝gcc編譯器,直接執行:gcc -v,判斷是否安裝gcc。
⑦ Linux編譯C語言,源程序文件放在什麼路徑
改名:mv hello hello.c
編譯:首先cd到你源文件的那個目錄下,這樣省去了指定絕對路徑的麻煩,比如cd到桌面,然後:gcc hello.c -o hello
運行:./hello
⑧ 在linux系統下怎麼用編譯工具鏈編譯源代碼
一般是用的GNU的那一套工具鏈
比如你寫了一個源碼為main.c
然後gcc main.c即可,生成的a,out為可執行的ELF文件。
如果你需要了解GCC的詳解資料,最好讀一下它的man pages
⑨ Linux下如何編譯並運行C程序
1.編譯單個源文件 [例如]在屏幕上列印"Hello,Linux." [源代碼]#include<stdio.h #include<stdlib.h int main(int argc,char **argv){printf("Hello,Linux.\n");exit(0);}將源文件保存為hello.c,開始進行編譯 $gcc -o hello hello.c 編譯成功完成後,在當前路徑下,生成一個名為hello的文件,然後執行 $./hello在屏幕上,你將會看到列印結果:Hello,Linux. 說明:在默認情況下,編譯成功完成後,會在當前路徑下,生成一個名為a.out的文件,然後執行$./a.out便可列印結果,但通常可以通過選項-o來指定自己的可執行程序名稱; 2.編譯多個源文件 3.使用外部函數庫 4.使用共享函數庫和靜態函數庫5.gcc選項詳細描述