深入理解C語言中編譯相關(guān)的常見錯誤
更新時間:2013年05月22日 14:55:55 作者:
本篇文章是對C語言中編譯相關(guān)的常見錯誤進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
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,其中會定義它所提供的頭文件在哪里,有哪些,其庫鏈接方式是怎么樣,庫在哪里,當(dāng)然這都是庫安裝到系統(tǒng)以后的信息,換句話說,可能對于編譯環(huán)境是無意義的。
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
出現(xiàn)這種錯誤的時候通常是由于具體使用了某種類型,但此類型(到使用的時候)還僅有聲明,未有定義。比如說,某個頭文件有如下聲明:
#ifndef __POINT_H
#define__POINT_H
typedef struct _Point Point;
#endif
如果包含了此頭文件的文件,可以使用Point去聲明:
1).如聲明函數(shù)時的形式參數(shù),void print_point(Point p),注意是聲明函數(shù)時,而不是定義函數(shù)
2). 聲明指針:Point *p;
但是不能使用Point去定義變量,
1). 如定義變量,Point p;
2). 定義函數(shù)時的形參,void print_point(Point p) { ... }
3) .或者為其指針申請內(nèi)在空間時,Point *point = (Point *) calloc(1, sizeof(Point));
會報(bào)出incomplete type的編譯錯誤。因?yàn)檫@個時候需要Notification所占的內(nèi)存大小和具體的定義形式,但是頭文件中并沒有給出具體的定義,所以編譯器不知道此類型所需要的內(nèi)存,所以會編譯出錯。
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;
....
其實(shí)原因也很簡單,當(dāng)令需要某個類型來聲明變量時,不需分配內(nèi)存,不需要對其進(jìn)行操作,自然就不用了解其具體的類型定義。但當(dāng)你使用時,要分配內(nèi)存時,就必須要了解類型是怎么定義的,否則這些操作無法完成,這自然就需要知道類型的具體定義。
其實(shí),在頭文件中僅聲明類型的目的是為了信息隱藏,也就是不讓調(diào)用者知道這個類型具體的定義是什么樣子的,那么就需要像Java/C++中那樣去定義這個類型,
1) 把類型聲明為指針類型:
typedef struct Point *Point;
否則調(diào)用者還是有可能去定義。
2) 也即在頭文件的對應(yīng)源文件中封裝操作此類型的所有方法,這樣外界就沒有必要去了解類型是如何定義的了。它想操作時,僅需要調(diào)用封裝的方法即可。
典型的實(shí)例:
頭文件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
實(shí)現(xiàn)源文件: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);
}
(.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,其中會定義它所提供的頭文件在哪里,有哪些,其庫鏈接方式是怎么樣,庫在哪里,當(dāng)然這都是庫安裝到系統(tǒng)以后的信息,換句話說,可能對于編譯環(huán)境是無意義的。
復(fù)制代碼 代碼如下:
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
出現(xiàn)這種錯誤的時候通常是由于具體使用了某種類型,但此類型(到使用的時候)還僅有聲明,未有定義。比如說,某個頭文件有如下聲明:
復(fù)制代碼 代碼如下:
#ifndef __POINT_H
#define__POINT_H
typedef struct _Point Point;
#endif
如果包含了此頭文件的文件,可以使用Point去聲明:
1).如聲明函數(shù)時的形式參數(shù),void print_point(Point p),注意是聲明函數(shù)時,而不是定義函數(shù)
2). 聲明指針:Point *p;
但是不能使用Point去定義變量,
1). 如定義變量,Point p;
2). 定義函數(shù)時的形參,void print_point(Point p) { ... }
3) .或者為其指針申請內(nèi)在空間時,Point *point = (Point *) calloc(1, sizeof(Point));
會報(bào)出incomplete type的編譯錯誤。因?yàn)檫@個時候需要Notification所占的內(nèi)存大小和具體的定義形式,但是頭文件中并沒有給出具體的定義,所以編譯器不知道此類型所需要的內(nèi)存,所以會編譯出錯。
C++中也是如此,為了效率會Forward declaration,也即在使用某個類前,不具體指定其類,而是聲明一個沒有定義的類:
class Point;
Point a;
使用Foward declaration時,也只能用其去聲明,而不能具體使用此類型。
所以,如果要具體使用某個類型時,其所包含的頭文件中必須要有類型的具體定義:
復(fù)制代碼 代碼如下:
#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;
....
其實(shí)原因也很簡單,當(dāng)令需要某個類型來聲明變量時,不需分配內(nèi)存,不需要對其進(jìn)行操作,自然就不用了解其具體的類型定義。但當(dāng)你使用時,要分配內(nèi)存時,就必須要了解類型是怎么定義的,否則這些操作無法完成,這自然就需要知道類型的具體定義。
其實(shí),在頭文件中僅聲明類型的目的是為了信息隱藏,也就是不讓調(diào)用者知道這個類型具體的定義是什么樣子的,那么就需要像Java/C++中那樣去定義這個類型,
1) 把類型聲明為指針類型:
typedef struct Point *Point;
否則調(diào)用者還是有可能去定義。
2) 也即在頭文件的對應(yīng)源文件中封裝操作此類型的所有方法,這樣外界就沒有必要去了解類型是如何定義的了。它想操作時,僅需要調(diào)用封裝的方法即可。
典型的實(shí)例:
頭文件point.h:
復(fù)制代碼 代碼如下:
#ifndef __POINT_H
#define __POINT_H
typedef struct _Point *Point;
Point make_point();
void print_point(Point point);
void destroy_point(Point p);
#endif
實(shí)現(xiàn)源文件:point.c
復(fù)制代碼 代碼如下:
#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);
}
相關(guān)文章
C語言中函數(shù)指針與軟件設(shè)計(jì)經(jīng)驗(yàn)總結(jié)
今天小編就為大家分享一篇關(guān)于C語言中函數(shù)指針與軟件設(shè)計(jì)經(jīng)驗(yàn)總結(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
ubuntu20.04中vscode使用ROS的詳細(xì)方法
這篇文章主要介紹了ubuntu20.04?vscode使用ROS的詳細(xì)方法,主要包括在vscode安裝擴(kuò)展創(chuàng)建工作文件夾的相關(guān)知識,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
C語言連續(xù)生成多個隨機(jī)數(shù)實(shí)現(xiàn)可限制范圍
這篇文章主要介紹了C語言連續(xù)生成多個隨機(jī)數(shù)實(shí)現(xiàn)可限制范圍,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Visual Studio Code上添加小程序自動補(bǔ)全插件的操作方法
這篇文章主要介紹了Visual Studio Code上添加小程序自動補(bǔ)全插件的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
c++報(bào)錯問題解決方案lvalue required as left opera
這篇文章主要介紹了c++報(bào)錯:lvalue required as left operand of assignment,出現(xiàn)此錯誤原因,是因?yàn)?,等號左邊是不可被修改的表達(dá)式或常量,而表達(dá)式或常量不能作為左值,需要的朋友可以參考下2023-01-01

