① 怎么在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选项详细描述