2014-09-21 54 views
0

我試圖編寫一個簡單的D程序並使用它來訪問一個簡單的C庫,但存在未知錯誤。D訪問CentOS 6.5上的C庫

我的C代碼,Box.c 的#include 「Box.h」

int SayHello(int _int) 
{ 
    _int ++; 
    return _int; 
} 

我的C頭文件,Box.h

#ifndef BOX_H_INCLUDED 
#define BOX_H_INCLUDED 

/* export interfaces */ 
#ifdef __cplusplus 
extern "C" { 
#endif 

int SayHello(int _int); 

#ifdef __cplusplus 
} 
#endif 

#endif // BOX_H_INCLUDED 

我編譯

gcc -c Box.c Box.h 

生成的文件

Box.o 
Box.h.gch 

我把它們放在我的d計劃的項目目錄

我d代碼

module main; 

import std.stdio; 
import std.conv; 
import std.c.stdio; 
import clib; 

int main(string[] args) 
{ 
    // test external c library 
    auto s = to!string( SayHello(3)) ; 
    writefln("my int is "~ s); 
    readln(); 
    return 0; 
} 

我d接口文件(CLIB),試圖鏈接到我的C庫

module clib; 

import std.c.stdio; 

extern (C) int SayHello(int _int); 

的使用代碼塊編譯時出現的錯誤

Compiling: hello.d 
Linking console executable: bin/Debug/tutorial03-access-c-library4 
obj/Debug/hello.o: In function `_Dmain': 
/home/hamilton/Tutorial/tutorial03-access-c-library4/hello.d:11: **undefined reference to `SayHello'** 
collect2: ld returned 1 exit status 
Process terminated with status 1 (0 minutes, 0 seconds) 
0 errors, 0 warnings 

錯誤是「未定義的引用'的SayHello「」

沒有錯誤,我得到當我編譯它使用命令控制檯

$ dmd Box.o hello.d clib.di 

這將是非常痛苦的,如果我需要,我不能使用代碼塊調試功能。 感謝

更新:

爲隨後 鏈接的動態庫中的代碼塊

編譯器設置:GCC -m32 -lrt 鏈接靜態庫:AR 調試器:GDB

回答

0

可以更改構建選項在CodeBlocks中從Project -> Build Options,Compiler settings -> Other options。最簡單的做法是將Box.o添加到Other options

+0

嗯...它不起作用,結果是一樣的 – 2014-09-21 06:25:36

+0

你確定新的構建完成了嗎?如果在本地嘗試,如果源代碼中沒有任何更改,則不會執行新的構建。 – yaz 2014-09-21 06:48:16

+0

並確保選擇的編譯器是Digital Mars D Compiler。 – yaz 2014-09-21 06:49:11