请教Linux下arduino编译时出错出错的问题

5606人阅读
linux学习笔记(19)
UNIX环境高级编程
前几天买了《UNIX环境高级编程》这本书,想好好学习下linux的编程。谁知道看到第一个列出指定目录的内容的那个例子,其实就是shell中 ls 的内容,打好代码要运行时一直出问题。后来在网上找了挺多的解决方法,终于解决了。先把方法贴上。
先在终端里面输入 vi ls.c在里面编辑如下代码
#include &apue.h&
#include &dirent.h&
int main(int argc, char *argv[])
struct dirent
if (argc != 2)
err_quit(&usage: ls directory_name&); //请输入文件名
if ((dp = opendir(argv[1])) == NULL)
err_sys(&can't open %s&, argv[1]);
//不能打开文件
while ((dirp = readdir(dp)) != NULL)
printf(&%s\n&, dirp-&d_name);
closedir(dp);
再保存退出.进行编译 &如gcc ls.c会出现如下的错误。
ls.c:1:19: apue.h: No such file or directory
ls.c: In function `main':
ls.c:13: error: `NULL' undeclared (first use in this function)
ls.c:13: error: (Each undeclared identifier is reported only once
ls.c:13: error: for each function it appears in.)
解决方法:
因为apue.h是作者自定义的一个头文件,包括程序所需的常用头文件及出错处理函数。所以因该将它放入系统头文件中(Linux下是&/usr/include),这样gcc编译器就可以找到它了。所以先去作者提供网站/apue2e.html下的
Suorce code 下&的src.2e.tar.gz包,然后解压至电脑中的某个目录,比如我的是在/home/xxx(你的登录名)/下,然后进入解压目录apue.2e,修改Make.defines.linux中的WKDIR=/home/xxx/apue.2e,为WKDIR=/home/user/apue.2e,这就是我们将要make的工作目录,然后再进入std目录,用vi打开linux.mk,将里面的nawk全部改为awk
接下里拷贝apue.h到系统默认头文件目录中(这个目录是/usr/include)
首先要先却换到root用户&
然后把apue.2e/include下的apue.h拷贝到&/usr/include
运行下面的命令 #cp /home/xxx/apue.2e/include/apue.h /usr/include
回到ls.c文件在的目录运行程序
会出现下面的错误:
: undefined reference to `err_quit'
: undefined reference to `err_sys'
解决办法:
因为err_quit跟err_sys是作者自己定义的错误处理函数,需要单独定义头文件
在/usr/include 下新建一个名为myerr.h的文件
里面的内容为
#include &apue.h&
#include &errno.h&
/* for definition of errno */
#include &stdarg.h&
/* ISO C variable aruments */
static void err_doit(int, int, const char *, va_list);
* Nonfatal error related to a system call.
* Print a message and return.
err_ret(const char *fmt, ...)
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
* Fatal error related to a system call.
* Print a message and terminate.
err_sys(const char *fmt, ...)
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
* Fatal error unrelated to a system call.
* Error code passed as explict parameter.
* Print a message and terminate.
err_exit(int error, const char *fmt, ...)
va_start(ap, fmt);
err_doit(1, error, fmt, ap);
va_end(ap);
* Fatal error related to a system call.
* Print a message, dump core, and terminate.
err_dump(const char *fmt, ...)
va_start(ap, fmt);
err_doit(1, errno, fmt, ap);
va_end(ap);
/* dump core and terminate */
/* shouldn't get here */
* Nonfatal error unrelated to a system call.
* Print a message and return.
err_msg(const char *fmt, ...)
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
* Fatal error unrelated to a system call.
* Print a message and terminate.
err_quit(const char *fmt, ...)
va_start(ap, fmt);
err_doit(0, 0, fmt, ap);
va_end(ap);
* Print a message and return to caller.
* Caller specifies &errnoflag&.
static void
err_doit(int errnoflag, int error, const char *fmt, va_list ap)
buf[MAXLINE];
vsnprintf(buf, MAXLINE, fmt, ap);
if (errnoflag)
snprintf(buf+strlen(buf), MAXLINE-strlen(buf), &: %s&,
strerror(error));
strcat(buf, & &);
fflush(stdout);
/* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(NULL);
/* flushes all stdio output streams */
接下来在ls.c里面引用#include&myerr.h&就好了。
在运行程序
./a.out /home
就可以看到你home目录下面的内容了
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:945883次
积分:7086
积分:7086
排名:第3399名
原创:123篇
转载:40篇
评论:116条
向优秀的人对齐
(2)(2)(1)(3)(8)(6)(5)(8)(11)(10)(8)(36)(26)(18)(19)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'2016年12月 其他开发语言大版内专家分月排行榜第二
2016年12月 其他开发语言大版内专家分月排行榜第二
2017年9月 C/C++大版内专家分月排行榜第三2017年6月 C/C++大版内专家分月排行榜第三2017年5月 C/C++大版内专家分月排行榜第三2017年4月 C/C++大版内专家分月排行榜第三2017年3月 C/C++大版内专家分月排行榜第三
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。shell下function提示出错啊!-红联Linux系统门户
您的位置:
&& 查看内容 - - -
shell下function提示出错啊!
&&字号: &&&&(网友评论&7&条)&
第一次提问,请大家见谅!
我输入最简单的function函数:
#!/bin/bash
#hello-fun
function hello()
echo "hello,today is `date`"
提示以下错误:
ymj@ymj-laptop:~/scripts$ sh hello_function.sh
hello_function.sh: 3: Syntax error: "(" unexpected
怎么解决?谢谢!
作者: shenhao0129&发布日期:
帮你修改了一下,下面是正确的结果CODE:#!/bin/bash
#hello-fun
function hello()
echo &hello,today is `date`&
hello运行结果如下:CODE:hello,today is 2010年 10月 04日 星期一 11:57:57 CST
作者: &发布日期:
谢谢你,不过没解决@@,哎,求解决,感谢ing。。。
作者: shenhao0129&发布日期:
.......我贴出来的代码都能正确的得到运行的结果,难道你贴上不不能用吗?
很明显你是只写出来那个函数来,并没有调用任何地方调用这个函数
作者: &发布日期:
我是将你的代码复制到里面的,运行时和之前的一样!
整个过程是这样:我输入vi hello_function.sh
#!/bin/bash
#hello-fun
function hello()
echo "hello,today is `date`"
sh hello_function.sh
hello_function.sh: 3: Syntax error: "(" unexpected
我试着把最后一个hello去掉,结果一样。
我看鸟哥的书,编里面的一个function函数的例子,也是这个错误!
所以才来请教!
请帮忙解决一下,谢谢!
作者: shenhao0129&发布日期:
不要用sh来运行,直接用chmod给hello_function.sh加上执行属性,然后./hello_function.sh来运行就好了,目前在我的archlinux上运行成功
作者: &发布日期:
恩,成了,呵呵,谢谢啊!这样是运行成了,跟你说的一样!
哇,好不容易啊,还要努力@@,呵呵
作者: &发布日期:
估计是shell版本的问题吧,鸟哥有些运行出来确实报错。把funtion关键字去掉就OK了,比如
#!/bin/bash
test_func() { echo "yes" }
共有评论数 7/每页显示数 10
发表评论,与各位同人交流。回复请点击下方的我要评论按钮(游客可回复),要发表贴子请点击
Linux教程下载?“”(请点击),Linux教程免费下载。
求助Linux问题?论坛有39版块,覆盖所有Linux技术层面。前往“”
 |  |  |  |  |  |  |  |  |  |  |  | 
&2017 红联 Powered by SupSite博客访问: 60036
博文数量: 27
博客积分: 674
博客等级: 中士
技术积分: 269
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: LINUX
在做linux的时候会出很多错误,于是自己就在网上查找原因与解决的办法。在这里将其整理,防止再次发生同样的错误。
1、drivers/video/console/vgacon.c:292: error: `PCIMEM_BASE ' undeclared (first use in this function)
解决方法:device drivers-&&& Graphics support-&&&&&& Console display driver support-&&&&&&&&& [ ]VGA text console
是signed size_t,之前有:typedef& int& ssize_t;
是标准C库中定义的,应为unsigned int
阅读(571) | 评论(2) | 转发(0) |
相关热门文章
给主人留下些什么吧!~~
用gcc编译c程序的时候 经常会出现
implicit declaration of function '...' 的warning
经过在网上搜索,发现主要有2种情况会产生这种warning
1& &没有把函数所在的c文件生成.o目标文件
2& &在函数所在的c文件中定义了,但是没有在与之相关联的.h文件中声明
在执行shell脚本时, 提示 autoreconf: not found
是因为 没有安装 automake, just to do below:
&&sudo apt-get install autoconf automake libtool
请登录后评论。

我要回帖

更多关于 vc 6.0编译出错 的文章

 

随机推荐