c - CentOS 6.5上D访问C库

标签 c codeblocks d

我尝试编写一个简单的 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/

相关文章:

operator-overloading - D 中优雅的运算符重载

c - goto 指令跳到变长数组前面

c - 将 Flex 和 Bison 生成的 yyparse() 嵌入到用 C 语言编写的 CLI 程序中

c++ - 在 Windows XP 和 CodeBlocks 上延迟加载 DWMAPI

d - 如何检查类型是否为无符号?

d - 是否可以在不运行它们的情况下编译 unittest 并为特定模块显式运行 unittest?

c - 将条件表示为公式

c++ - 使用 std::chrono 将 32 位 unix 时间戳转换为 std::string

c++ - 如何在 codeBlocks 项目中链接 .o 文件?

c++ - Visual Studio 错误。它在 CodeBlocks 中工作...要求 const