[英]How do you properly link to a static library using g++
解決方案:感謝所有對此問題發表評論的人,但我在另一個論壇上解決了這個問題,並認為我會在這里為任何有相同問題的人發布答案。
所以,我想只有動態庫才能使用__declspec(dllexport),所以當你嘗試創建一個靜態庫時,會導出這些方法(名稱需要被修改為c ++兼容),所以在聲明extern“C”時__declspec ....你最終得到了在嘗試靜態鏈接時無法識別的方法名稱。
所以,簡單的修復.....刪除__declspec
我有2個項目,一個是靜態庫,另一個是win32應用程序。
我只是想將我創建的庫包含在我的win32應用程序中,但是g ++不斷給我這個錯誤:
../MyLib/TestClass.h:16:未定義引用` imp __ZTV9TestClass'
這是我在嘗試編譯應用程序時遇到的錯誤,即使該文件是庫的一部分。
我試圖在嘗試查找錯誤時盡可能創建此項目的最簡化版本。
以下是兩個項目的源文件:
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 */
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");
}
最后,實現庫的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 ++選項對我-lName
。 不要在path/to/library
指定庫名。
嘗試在鏈接命令行中的main.o之前放置-L和-l。
解決方案:感謝所有對此問題發表評論的人,但我在另一個論壇上解決了這個問題,並想出我會在這里為任何有同樣問題的人發布答案。
所以,我想只有動態庫才能使用__declspec(dllexport)
,所以當你嘗試創建一個靜態庫時,會導出這些方法(名稱需要被修改為C ++兼容),所以在聲明extern "C" __declspec....
你最終得到了在嘗試靜態鏈接時無法識別的方法名稱。
所以,簡單修復:刪除__declspec
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.