How to reference a static library using g ++

Solution: Thanks to everyone who commented on this problem, but I solved it in another forum, and decided that I would post an answer here for those who have the same problem.

So, I think that only dynamic libraries use __declspec (dllexport), so when you try to create a static library, the methods are exported (names must be mutilated in order to be compatible with C ++), therefore, when declaring extern "C" __declspec. ... you will get method names that are not recognized when you try to bind statically.

So, a simple fix ..... remove __declspec

I have 2 projects, one of them is a static library, the other is just a win32 application.

I just want to include the library that I created in my win32 application, however g ++ continues to give me this error:

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

This is the error I get when trying to compile the application, although this file is part of the library.

I tried to simplify the version of this project as much as possible, trying to find an error.

Here are the source files for both projects:

MyLib.h is the main include file for clients that references functions in the library

#ifndef MYLIB_H #define MYLIB_H #include "libexport.h" #include "TestClass.h" #endif /* MYLIB_H */ 

libexport.h - A fairly general file for defining import / export keywords

 #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"); } 

And finally, a win32 application that implements the library:

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; } 

The compilation library is error-free, however, here is the output when compiling the main application:

 g++.exe -c -g -MMD -MP -MF build/Debug/MinGW-Windows/main.od -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) 

Most of the other posts I've seen on this topic say that the problem is with the binding order, but even after adding -lmylib to the beginning of the compiler build line, the same errors persist:

 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' 

I really need help with this, I created many dynamic libraries before using the above code, and it works without problems, I don’t understand why I have so many problems creating a simple static library. Any help is appreciated.

+4
source share
3 answers

Solution: Thanks to everyone who commented on this problem, but I solved it in another forum, and realized that I would send an answer here for those who have the same problem.

So, I think that only dynamic libraries use __declspec(dllexport) , therefore when trying to create a static library the methods are exported (names must be mutilated to be compatible with C ++), therefore, when declaring extern "C" __declspec.... you get method names that are not recognized when you try to bind statically.

So, a simple fix: remove __declspec

+1
source

-L/path/to/library/ and -lName , since g ++ options worked for me. Do not include the library name in path/to/library .

+2
source

Try putting -L and -l in front of main.o on the link command line.

+1
source

Source: https://habr.com/ru/post/1393947/


All Articles