设为首页 加入收藏

TOP

C语言中编译相关的常见错误(一)
2013-03-05 13:22:26 来源: 作者: 【 】 浏览:1324
Tags:言中 编译 相关 常见 错误

  1、/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':

  (.text+0x18):undefined reference to `main'

  collect2: ld 返回 1

  Reason: no main function in source file

  2、to get compile options -I and -l

  pkg-config lib

  e.g: pkg-config --cflags --libs dbus-1 glib-2.0 dbus-glib-1

  gcc -o send-sms send-sms.c `pkg-config --cflags --libs dbus-1 glib-2.0 dbus-glib-1`

  3、如何让pkg-config找到自己写的库

  在库中有一个文件libxxx.pc.in,其中会定义它所提供的头文件在哪里,有哪些,其库链接方式是怎么样,库在哪里,当然这都是库安装到系统以后的信息,换句话说,可能对于编译环境是无意义的。

  prefix=@PREFIX@

  exec_prefix=${prefix}

  libdir=${exec_prefix}/lib

  includedir=${prefix}/include

  Name: library name

  Description: description goes here

  Requires: glib-2.0 gobject-2.0

  Version: 0.1

  Libs: -L${libdir} -llibrary_name

  Cflags: -I${includedir}/some_sub_dir

  这个文件定义了安装后此库的所有信息,而pkg-config就会读取此信息。

  4、forward declaration and incomplete type

  出现这种错误的时候通常是由于具体使用了某种类型,但此类型(到使用的时候)还仅有声明,未有定义。比如说,某个头文件有如下声明:

  #ifndef __POINT_H

  #define__POINT_H

  typedef struct _Point Point;

  #endif

  如果包含了此头文件的文件,可以使用Point去声明:

  1)如声明函数时的形式参数,void print_point(Point p),注意是声明函数时,而不是定义函数

  2)声明指针:Point *p;

  但是不能使用Point去定义变量,

  1)如定义变量,Point p;

  2)定义函数时的形参,void print_point(Point p) { ... }

  3)或者为其指针申请内在空间时,Point *point = (Point *) calloc(1, sizeof(Point));

  会报出incomplete type的编译错误。因为这个时候需要Notification所占的内存大小和具体的定义形式,但是头文件中并没有给出具体的定义,所以编译器不知道此类型所需要的内存,所以会编译出错。

  C++(www.cppentry.com)中也是如此,为了效率会Forward declaration,也即在使用某个类前,不具体指定其类,而是声明一个没有定义的类:

  class Point;

  Point a;

  使用Foward declaration时,也只能用其去声明,而不能具体使用此类型。

  所以,如果要具体使用某个类型时,其所包含的头文件中必须要有类型的具体定义:

  #ifndef __POINT_H

  #define __POINT_H

  typedef struct _Point Point;

  struct _Point {

  int x;

  int y;

  };

  #endif

  #include "point.h"

  Point *n = (Point *) calloc(1, sizeof(Point));

  n->x = 1;

  n->y = 2;

  ....

   

首页 上一页 1 2 3 4 5 下一页 尾页 1/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C:在终端里输出正玄余玄 下一篇C语言指针数组与数组指针的学习

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: