我尝试编写一个简单的 D 程序并使用它来访问一个简单的 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 接口(interface)文件 ( 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
最佳答案
您可以通过项目 -> 构建选项
、编译器设置 -> 其他选项
更改 CodeBlocks 中的构建选项。最简单的方法是将 Box.o
添加到 Other options
中。
关于c - CentOS 6.5上D访问C库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25955898/