Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic link library does not generate a .lib file when compiled (Visual Studio C++ Express)

As part of learning C++, I wrote a simple class library + application that references it. Everything builds, except the class library does not generate a .lib file, which results in the application throwing a "LINK : fatal error LNK1104: cannot open file". This seems very reasonable; obviously, if a necessary file isn't there, there's an error and it's fatal. (Side note: I don't have a book yet)

So, I went looking for reasons a .lib file might not be generated. My search-fu, by the way, is rather weak. All I did find was that, if the library did not have any __declspec(dllexport) tags, it would not export a .lib.

I shall now post the header and .cpp contents of the class library (A simple "Console" class with one "Write(std::string)" method).

Header:

// Extensions.h

#pragma once

#include "stdafx.h"

namespace Extensions {

    __declspec(dllexport) class Console
    {
    public:
        __declspec(dllexport) static void Write(std::string text);
    };
}

I am unsure whether I need to tag the function when I've tagged the class, but I can check that when it works.

And the .cpp file:

// This is the main DLL file.

#include "stdafx.h"

// #include "Console.h"

namespace Extensions {

    void Console::Write(std::string text)
    {
        std::cout << text.c_str();
    }
}

I've checked and it is set to generate a dynamic link library.

Thanks.

like image 929
Narf the Mouse Avatar asked Feb 16 '11 17:02

Narf the Mouse


People also ask

How do I create a .LIB file in Visual Studio?

To create a static library project in Visual StudioOn the menu bar, choose File > New > Project to open the Create a New Project dialog. At the top of the dialog, set Language to C++, set Platform to Windows, and set Project type to Library.

What is .LIB file in Visual Studio?

The Microsoft Library Manager (LIB.exe) creates and manages a library of Common Object File Format (COFF) object files. LIB can also be used to create export files and import libraries to reference exported definitions. You can start this tool only from the Visual Studio command prompt.

What is a .LIB file in C++?

lib file contains all the code and data for the library. The linker then identifies the bits it needs and puts them in the final executable. For a dynamic library, the . lib file contains a list of the exported functions and data elements from the library, and information about which DLL they came from.


1 Answers

Here is some sample code that demonstrates how to correctly export a class. Pay attention to the CONSOLETEST_EXPORT macro. This is the missing part of your solution. You need to define this macro in your DLL project, and leave it undefined in the projects that reference this dll.

// MAIN.CPP - TestApplication

#include <iostream>
#include "ConsoleTest.h"

int main(int argc, char** argv)
{
    std::cout << "Hello World" << std::endl;

    ConsoleTest test;

    test.Write();
    ConsoleTest::StaticWrite();

    system("pause");
}


// ConsoleTest.h - TestDll 

#include <iostream>

#ifdef CONSOLETEST_EXPORT
    #define CONSOLETEST_API __declspec(dllexport)
#else
    #define CONSOLETEST_API __declspec(dllimport)
#endif

class CONSOLETEST_API ConsoleTest
{
public:
    ConsoleTest();
    ~ConsoleTest();
    void Write();
    static void StaticWrite();
};


// ConsoleTest.cpp - TestDll

#include "ConsoleTest.h"

ConsoleTest::ConsoleTest()
{
}

ConsoleTest::~ConsoleTest()
{
}

void ConsoleTest::Write()
{
    std::cout << "Instance Write" << std::endl;
}

void ConsoleTest::StaticWrite()
{
    std::cout << "Static Write" << std::endl;
}

Check out this article on codeproject for more details. HowTo: Export C++ classes from a DLL

like image 124
Nathanael Avatar answered Sep 28 '22 03:09

Nathanael