2
votes

After creating a GUI for my personal little application, I am trying to compile it as a static library to use in another project. The GUI is created and has other tasks performed with it through a managed public ref class that currently only has 5 functions, but will be added to.

This is my header file in the static library that would be included in the other project (well, at least a new one with only the public functions, all the extra stuff would be removed);

#ifndef GUIInterface_H
#define GUIInterface_H
#include 
#include "MainForm.h"

using namespace System;
using namespace System::Threading;

 public ref class GUIInterface
{
private:
    ThreadStart^ GUIThreadDelegate;
    Thread^ GUIThread;
    void GUIThreadFunction()
    {
        ANNGUI::Application::EnableVisualStyles();
        ANNGUI::Application::Run(gcnew ANNGUI::MainForm());
        return;
    }

public:
    int CreateGUI();

    int DestroyGUI();

    int GetInputData();

    bool CheckNewInput();

    int NetworkState();
};
#endif

This is how it would be accessed from the other project (.exe)

#include "main.h"

int main()
{
    GUIInterface ^GUI = gcnew(GUIInterface);
    GUI->CreateGUI();
    return 0;
}

The main.h just contains the class definitions like the header file shown above, minus the private stuff.

I am using Visual Studio 2010 Express C++. Both projects are compiled with /clr switch. The static library is set as a reference in the .exe project. When I compile the static library as a DLL instead, the program runs perfectly (after I remove the main.h in the application). When it is compiled as a static library I get a LNK2020 Error for each of the functions used to communicate with the GUI.

I linked the .exe with the /VERBOSE option to see the output. The linker looks exactly where the .lib is multiple times, but never says it found it even though it's looking EXACTLY in the right path and for the right file. Also, if it means anything, my .lib builds fine but gets this as a warning.

.NETFramework,Version=v4.0.AssemblyAttributes.obj : warning LNK4221: This object file does not define any previously undefined public symbols, so it will not be used by any link operation that consumes this library

My question is, why is the linker looking in the right place for the .lib, but not seeing it right in front of it's face? How can I go about fixing this so that I can use my static .lib?

1

1 Answers

4
votes

A static .lib is not a valid target option for managed code. There is no implemented mechanism to get the all-important metadata associated with managed types the same kind of treatment as code. It cannot be carved up in bits in pieces and glued together again. The MSFT team probably considered it but got scared away by the massive changes required to the .obj file format. That's a guess. A good error message would have been nice. That's a fact.

You must select a DLL as the target. At runtime, the JIT compiler performs the same function as the linker, it only pulls code from the DLL assembly that is actually used by the client.