Jump to content
  • Advertisement
Sign in to follow this  
FoxC

Problems with static library

This topic is 2511 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Edit:


I posted a simplified version of this question on stackoverflow that may help:


http://stackoverflow.com/questions/9078513/how-do-you-properly-link-to-a-static-library-using-g



Okay, so I've written dynamic libraries before and haven't run into any problems, but now that I'm trying to write a static library, I'm getting problems saying that the methods aren't found, like they aren't being exported/imported correctly. To be specific, here is the error I am receiving when trying to use the static library:




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

"..MyClient/Main.cpp:15: undefined reference to `_imp___ZN6SystemC1Ev'"[/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Basically what I am trying to do is create a static library that can access my dynamic libraries (via interfaces and such, trying to create a more modular engine)[/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Here are the files I have for my static library, followed by the Main.cpp files of my client test application:[/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

libexport.h (Generic file used to define import/export functions, I've always used this file in my DLL projects...)[/font]


#ifndef LIBEXPORT_H
#define LIBEXPORT_H
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif

#ifdef LIB_EXPORT
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
}
#endif
#endif /* LIBEXPORT_H */



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

System.h (Defines a class intended for retrieving a 'Device' class, which I have in another project which is a dynamic lib..)[/font]


#ifndef SYSTEM_H
#define SYSTEM_H
#include "Device.h" // Not relevant to the question at hand, just an interface for inherited device classes
#include "libexport.h"
class DLL_EXPORT System
{
public:
System();
virtual ~System();

bool CreateDevice();

protected:
HINSTANCE m_hDLL;
};
#endif /* SYSTEM_H */



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

System.cpp[/font]


#define LIB_EXPORT
#include "System.h"
System::System()
{
}
System::~System()
{
}
bool System::CreateDevice()
{
m_hDLL = LoadLibrary("libMyDynamic.dll");

if(!m_hDLL)
{
return false;
}

FreeLibrary(m_hDLL);

return true;
}



[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

Main.cpp (And last but not least, the implementation of the static library within my test client)[/font]


#include <windows.h>
#include "../MyStatic/System.h"
#pragma comment(lib, "libmystatic.a")
int __stdcall WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
System *mySystem = new System();

delete mySystem;
mySystem = 0;
return 0;
}


[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

I'm just confused, I've never really worked with strictly static libraries (at least making them), I created this project EXACTLY as I would have if creating a dynamic library, and it's always seemed to work fine, what am I doing wrong? I'm sure it's something simple that I am overlooking.[/font]




[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

FYI I am also using the MinGW toolset to compile.[/font]

Share this post


Link to post
Share on other sites
Advertisement
As far as I know, __declspec(dllexport) is used only for dynamic linking. For static linking you don't need to specify anything.
Secondly static link means there will be no .dll file, so you have nothing to load.
When you link, specify library that was created, along with your code that uses library (similar way like you would link DirectX or other libraries).

Share this post


Link to post
Share on other sites
Wow, after hours of frustration and getting tons of horrible answers on stackoverflow, your the first person to give a legitimate answer, I simply removed the __declspec(dllexport) from my library, and viola! it works! I've only ever created dynamic libs, never static, so I was tearing my hair out trying to figure out what I was doing wrong... Thanks man!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!