C++ 如何使用g+正确链接到静态库+;

C++ 如何使用g+正确链接到静态库+;,c++,static,linker,g++,C++,Static,Linker,G++,解决方案: 感谢所有对这个问题发表评论的人,但我在另一个论坛上解决了这个问题,我想我会在这里为所有有同样问题的人发布答案 P> >,我想只有动态库使用了.Y-DESPECUT(DLLISTUM),所以当您尝试创建静态库时,方法被导出(名称需要被修改为C++兼容),因此当声明“外部”C“x”时,…在尝试静态链接时,最终会出现无法识别的方法名称 因此,简单的修复…删除u declspec 我有两个项目,一个是静态库,另一个只是win32应用程序 我只是想将我创建的库包含到我的win32应用程序中,

解决方案: 感谢所有对这个问题发表评论的人,但我在另一个论坛上解决了这个问题,我想我会在这里为所有有同样问题的人发布答案

<> P> >,我想只有动态库使用了.Y-DESPECUT(DLLISTUM),所以当您尝试创建静态库时,方法被导出(名称需要被修改为C++兼容),因此当声明“外部”C“x”时,…在尝试静态链接时,最终会出现无法识别的方法名称

因此,简单的修复…删除u declspec

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

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

../MyLib/TestClass.h:16:对“imp__ZTV9TestClass”的未定义引用

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

我已尝试创建此项目的最简化版本,以试图找到错误

以下是两个项目的源文件:

h-这是供客户端参考库中函数的主要包含文件

#ifndef MYLIB_H
#define MYLIB_H

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

#endif  /* MYLIB_H */
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 */
TestClass.h

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include "libexport.h"

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

    void TestFunc();
};

#endif  /* TESTCLASS_H */
TestClass.cpp

#define LIB

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

void TestClass::TestFunc()
{
    printf("This function was called from within the library.\n");
}
关于这个主题,我看到的大多数其他帖子都说问题在于链接顺序,但即使在编译器构建行的开头添加了-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和-L放在main.o之前。

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

解决方案:感谢所有对这个问题发表评论的人,但我在另一个论坛上解决了这个问题,我想我会在这里为所有有相同问题的人发布答案

<> P> >,我想只有动态库使用<代码> .yDeScript(DLLuturn)< /C> >,所以当您尝试创建静态库时,方法将导出(名称需要被修改为C++兼容),因此当声明“代码>外”“C”时_uudeclspec….在尝试静态链接时,最终会出现无法识别的方法名称


因此,简单的修复方法是:删除
\uu declspec

它是一个静态库,所以尝试使用
libmylib.a
(不带
-l
)而不是
-lmylib
。您可能必须指定完整路径:
build/Debug/libmylib.a
或其他任何内容。它似乎会生成相同的内容:g++.exe-mwindows../MyLib/dist/Debug/MinGW Windows/libmylib.a-o dist/Debug/MinGW Windows/testclient build/Debug/MinGW Windows/main.o-L../MyLib/dist/Debug/MinGW Windows-lmylib make[2]:离开目录
/c/Users/Nick/Documents/NetBeansProjects/TestClient'make[1]:离开目录
/c/Users/Nick/Documents/NetBeansProjects/TestClient'build/Debug/MinGW Windows/main.o:In function
TestClass:c:\Users\Nick\Documents\NetBeansProjects\TestClient/。/MyLib/TestClass.h:16:未定义对
\u imp\uuuuuuuuuu ZTV9TestClass'的引用链接对象和ar存档的顺序对nos来说很重要,最后,我只是简单地说,我一开始就尝试了它,没有新的结果,如果您查看提供的信息,它会有所帮助。将
。/MyLib/dist/Debug/MinGW Windows/libmylib.a
放在
build/Debug/MinGW Windows/main.o之后。
这正是我在给定示例中指定它的方式。。。“-L../MyLib/dist/Debug/MinGW Windows-lmylib”但它仍然给出了未定义的引用错误,这就是为什么我感到困惑的原因哦,对不起,一定是错过了它。
mylib
是否命名为
libmylib.a
?因为这是-l选项假定的。没有什么区别,仍然得到相同的未定义引用错误
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)
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'