gpt4 book ai didi

c++ - 如何使用 g++ 正确链接到静态库

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:56:21 24 4
gpt4 key购买 nike

Solution: Thanks to everyone who commented on this issue, but I resolved it on another forum, and figured I would post the answer here for anybody having the same issue.

So, I guess only dynamic libraries make use of __declspec(dllexport), so when you try to create a static library, the methods are exported (an the names need to be mangled to be c++ compatible), so when declaring extern "C" __declspec.... you end up with method names that aren't recognized when trying to link statically.

So, simple fix.....remove the __declspec

我有 2 个项目,一个是静态库,另一个只是一个 win32 应用程序。

我只是想将我创建的库包含到我的 win32 应用程序中,但是 g++ 一直给我这个错误:

../MyLib/TestClass.h:16: 对`imp__ZTV9TestClass' 的 undefined reference

这是我在尝试编译应用程序时遇到的错误,即使该文件是库的一部分。

我试图尽可能创建该项目的最简化版本以找出错误。

这是两个项目的源文件:

MyLib.h - 这是客户端引用库中函数的主要包含文件

#ifndef MYLIB_H
#define MYLIB_H

#include "libexport.h"
#include "TestClass.h"

#endif /* MYLIB_H */

libexport.h - 定义导入/导出关键字的非常通用的文件

#ifndef LIBEXPORT_H
#define LIBEXPORT_H

#ifdef __cplusplus
extern "C" {
#endif

#ifdef LIB
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif

#ifdef __cplusplus
}
#endif

#endif /* LIBEXPORT_H */

测试类.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include "libexport.h"

class DLL_EXPORT TestClass
{
public:
TestClass() {};
virtual ~TestClass() {};

void TestFunc();
};

#endif /* TESTCLASS_H */

测试类.cpp

#define LIB

#include <stdio.h>
#include "TestClass.h"

void TestClass::TestFunc()
{
printf("This function was called from within the library.\n");
}

最后,实现库的 win32 应用程序:

main.cpp

#include <windows.h>
#include "../MyLib/MyLib.h"

#pragma comment(lib, "libmylib.a")

int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

TestClass *myClass = new TestClass();

delete myClass;
myClass = 0;

return 0;
}

库编译没有错误,但是,这是编译主应用程序时的输出:

g++.exe    -c -g -MMD -MP -MF build/Debug/MinGW-Windows/main.o.d -o build/Debug/MinGW-Windows/main.o main.cpp
mkdir -p dist/Debug/MinGW-Windows
g++.exe -mwindows -o dist/Debug/MinGW-Windows/testclient build/Debug/MinGW-Windows/main.o -L../MyLib/dist/Debug/MinGW-Windows -lmylib
build/Debug/MinGW-Windows/main.o: In function `TestClass':
C:\Users\Nick\Documents\NetBeansProjects\TestClient/../MyLib/TestClass.h:16: undefined reference to `_imp___ZTV9TestClass'
make[2]: Leaving directory `/c/Users/Nick/Documents/NetBeansProjects/TestClient'
build/Debug/MinGW-Windows/main.o: In function `~TestClass':
make[1]: Leaving directory `/c/Users/Nick/Documents/NetBeansProjects/TestClient'
C:\Users\Nick\Documents\NetBeansProjects\TestClient/../MyLib/TestClass.h:17: undefined reference to `_imp___ZTV9TestClass'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/MinGW-Windows/testclient.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2


BUILD FAILED (exit value 2, total time: 1s)

我看到的大多数关于这个主题的其他帖子都说问题出在链接顺序上,但即使在将 -lmylib 添加到编译器构建行的开头之后,同样的错误仍然存​​在:

g++.exe -lmylib -mwindows -o dist/Debug/MinGW-Windows/testclient build/Debug/MinGW-Windows/main.o -L../MyLib/dist/Debug/MinGW-Windows -lmylib 
build/Debug/MinGW-Windows/main.o: In function `TestClass':
C:\Users\Nick\Documents\NetBeansProjects\TestClient/../MyLib/TestClass.h:16: undefined reference to `_imp___ZTV9TestClass'

我真的需要这方面的帮助,在使用上面的代码之前我已经构建了很多动态库,而且它没有问题,我不明白为什么我在构建一个简单的静态库时遇到这么多麻烦。非常感谢任何帮助。

最佳答案

-L/path/to/library/-lName 因为 g++ 选项对我有用。不要在 path/to/library 中指定库名称。

关于c++ - 如何使用 g++ 正确链接到静态库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9078513/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com