简体   繁体   English

Cygwin GCC与Visual Studio库链接

[英]Cygwin GCC linking with Visual Studio library

I've created a simple library (static 64bit - .lib) using Visual Studio 2012 Express. 我使用Visual Studio 2012 Express创建了一个简单的库(静态64位 - .lib)。 All this library has is one function: 所有这个库都有一个功能:

int get_number()
{ 
    return 67; 
}

Let's say that the produced lib is called NumTestLib64.lib . 假设生成的lib名为NumTestLib64.lib

I'm trying to compile a simple program (Let's call it test.cpp ) using Cygwin64 which will link NumTestLib64.lib and will print the result of get_number() : 我正在尝试使用Cygwin64编译一个简单的程序(让我们称之为test.cpp ),它将链接NumTestLib64.lib并将打印get_number()的结果:

#include <stdio.h>   

int get_number();

int main()
{
    printf("get_number: %d\n", get_number());
    return 0;
}

Pretty simple right? 很简单吧? Evidently not. 显然不是。
Compiling with g++ -o test test.cpp -L. -lTestLibStatic64 使用g++ -o test test.cpp -L. -lTestLibStatic64编译g++ -o test test.cpp -L. -lTestLibStatic64 g++ -o test test.cpp -L. -lTestLibStatic64 returns: g++ -o test test.cpp -L. -lTestLibStatic64返回:

/tmp/ccT57qc6.o:test.cpp:(.text+0xe): undefined reference to `get_number()'
/tmp/ccT57qc6.o:test.cpp:(.text+0xe): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `get_number()'
collect2: error: ld returned 1 exit status

and, g++ -o test test.cpp TestLibStatic64.lib returns: 并且, g++ -o test test.cpp TestLibStatic64.lib返回:

/tmp/ccMY8yNi.o:test.cpp:(.text+0xe): undefined reference to `get_number()'
/tmp/ccMY8yNi.o:test.cpp:(.text+0xe): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `get_number()'
collect2: error: ld returned 1 exit status

I'm looking for the brave ones who can provide instructions, both on the Visual Studio side and on the Cygwin command line side, as to how to get this thing done. 我正在寻找能够在Visual Studio方面和Cygwin命令行方面提供指令的勇敢者,以了解如何完成这项工作。

I've tried all the possible web pages, so probably links won't help, just exact instructions. 我已经尝试了所有可能的网页,所以可能链接无济于事,只是确切的说明。 I don't mind changing the library to DLL or perform any changes necessary, all code is mine, both in this simple example and in the actual application I'm developing. 我不介意将库更改为DLL或执行任何必要的更改,所有代码都是我的,在这个简单的示例和我正在开发的实际应用程序中。

Please help! 请帮忙!

Found the answer! 找到答案了! The key is creating both the *.dll and *.lib files. 关键是创建* .dll和* .lib文件。 The *.lib is created when symbols are actually exported. 实际导出符号时会创建* .lib。 Below is the header file of the created DLL (onlty worked when created DLL in Visual Studio, creating a static library doesn't work yet): 下面是创建的DLL的头文件(在Visual Studio中创建DLL时,onlty工作,创建静态库不起作用):

#ifdef TESTLIB_EXPORTS
#define TESTLIB_API __declspec(dllexport)
#else
#define TESTLIB_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
#endif

TESTLIB_API int get_num();

#ifdef __cplusplus
}
#endif

Of course, TESTLIB_EXPORTS is defined only in the DLL project. 当然, TESTLIB_EXPORTS仅在DLL项目中定义。 It's important that the main that links to this DLL will use this header, because of the __declspec(dllimport) part. 由于__declspec(dllimport)部分,链接到此DLL的main将使用此标头非常重要。 Also, extern "C" is a must, as the commentators suggested, to avoid mangling. 此外,正如评论员所建议的那样, extern "C"是必须的,以避免损坏。 Also, I've been successful in linking on Cygwin32 and MinGW32, and not on Cygwin64. 此外,我已成功连接Cygwin32和MinGW32,而不是Cygwin64。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM