程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> C語言基礎知識 >> 深入理解C語言中編譯相關的常見錯誤

深入理解C語言中編譯相關的常見錯誤

編輯:C語言基礎知識
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++中也是如此,為了效率會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;
....

其實原因也很簡單,當令需要某個類型來聲明變量時,不需分配內存,不需要對其進行操作,自然就不用了解其具體的類型定義。但當你使用時,要分配內存時,就必須要了解類型是怎麼定義的,否則這些操作無法完成,這自然就需要知道類型的具體定義。
其實,在頭文件中僅聲明類型的目的是為了信息隱藏,也就是不讓調用者知道這個類型具體的定義是什麼樣子的,那麼就需要像Java/C++中那樣去定義這個類型,
    1) 把類型聲明為指針類型:
    typedef struct Point *Point;
    否則調用者還是有可能去定義。
    2) 也即在頭文件的對應源文件中封裝操作此類型的所有方法,這樣外界就沒有必要去了解類型是如何定義的了。它想操作時,僅需要調用封裝的方法即可。
典型的實例:
頭文件point.h:
代碼如下:

#ifndef __POINT_H
#define __POINT_H
typedef struct _Point *Point;
Point make_point();
void print_point(Point point);
void destroy_point(Point p);
#endif

實現源文件:point.c
代碼如下:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "point.h"
struct _Point {
    int x;
    int y;
};
Point make_point() {
    Point point = (Point) calloc(1, sizeof(struct _Point));
    point->x = 0;
    point->y = 0;
    return point;
}
void print_point(Point point) {
    printf("point %d, %d\n", point->x, point->y);
}
void destroy_point(Point p) {
    if (p == NULL) {
         printf("warning, destroying NULL object");
         return;
    }
    free(p);
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved