C:如何从dll API调用函数?

时间:2011-03-09 16:19:30

标签: c api dll build

我正在尝试编写一个使用C API的程序。此API仅提供api.h标头文件,.dll.lib。 它正在使用__stdcall个出口。

我尝试添加api.h并添加.lib.dll文件,但我仍然对库路径有以下错误,但我仍然有以下错误:

**** Build of configuration Debug for project TestLibsp ****

**** Internal Builder is used for build               ****
gcc -LC:\Users\nbarraille\workspace\TestLibsp\lib -oTestLibsp.exe src\main.o
src\main.o: In function `main':
C:\Users\nbarraille\workspace\TestLibsp\Debug/../src/main.c:83: undefined reference to `sp_session_create@8'
C:\Users\nbarraille\workspace\TestLibsp\Debug/../src/main.c:86: undefined reference to `sp_error_message@4'
C:\Users\nbarraille\workspace\TestLibsp\Debug/../src/main.c:92: undefined reference to `sp_session_login@12'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 721  ms

这是我的.c文件:

#include "api.h"

int main(int argc, char **argv){
    sp_session *sp;
    sp_error err;
    sp_session_config spconfig;

    /* Create session */
    sp_error err = sp_session_create(&spconfig, &sp);
}

以下是我试图在api.h

中调用的函数的定义
#ifndef SP_LIBEXPORT
#ifdef _WIN32
#define SP_LIBEXPORT(x) x __stdcall
#else
#define SP_LIBEXPORT(x) x
#endif
#endif

SP_LIBEXPORT(sp_error) sp_session_create(const sp_session_config *config, sp_session **sess);

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

您需要使用gcc -l<libname> -L<libpath>链接库。我猜你正在使用eclipse,所以你可以在Project中轻松添加lib - &gt;属性 - &gt; C / C ++ Build - &gt;设置 - &gt; GCC C ++链接器 - &gt;文库

相关问题