2014-09-21 50 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