简体   繁体   中英

C++ DLL won't Link?

Alright, here's the issue. In an attempt to advance my knowledge of C++ APIs, I attempted to learn more about library file types. I have static libraries figured out quite well. You simply link the library into the project, and its contents are placed inside the binaries when it's compiled and linked. But, I hit an issue when I started trying to use dynamic libraries. I was able to successfully compile a DLL file using the __declspec function, and creating extern blocks(so it can successfully be exported to C++). But, the issue arises when I try to link the file. Based on multiple tutorials I have seen across the web, I create a header with forward definitions, and include it into the executable project. Then, I make sure to add the search directory to the project build settings, along with the path to the library I am importing. Then, when I go to build the project, it produces a linker error(I can tell because the object file is compiled) "cannot find -l[path to file]". Are there more configurations to be made? I assume it has something to do with my compiler(MinGW), because Code::Blocks(I'm currently using the Eclipse CDT plugin) produced a similar link error.

If you need to see the code, I will provide it below.

// EXE: main.cpp
#include <iostream>
#include "DLLTutorial.h"

int main()
{
    Function();
    std::cout << "1 + 3:\t" << Add(1, 3);
}


// DLL: DLLTutorial.cpp
#define DLL_EXPORT
#include <iostream>
#include "DLLTutorial.h"

extern "C"
{
    DLLCOMP int Add(int a, int b)
    {
        return a + b;
    };

    DLLCOMP void Function(void)
    {
        std::cout << "DLL Called!\n";
    };
};


// DLL: DLLTutorial.h
#ifndef DLLTUTORIAL_H_
#define DLLTUTORIAL_H_

#include <iostream>

#ifdef DLL_EXPORT
    #define DLLCOMP __declspec(dllexport)
#else
    #define DLLCOMP __declspec(dllimport)
#endif

extern "C"
{
    DLLCOMP int Add(int a, int b);
    DLLCOMP void Function(void);
};


#endif /* DLLTUTORIAL_H_ */

It's just very simple example code I used from a website. I cannot get the executable file to link properly, so I would appreciate any advice that could be given to me.

Also, I have a related question. Reading a few of the tutorials online, some made mention about the __declspec function being Windows/Microsoft specific. Is this true? In which case, what would be a nice equivalent to use on other operating systems?

When you build a dll you also create a companying .lib file, the 'implib'. You must statically link this file in your exe.

EDIT: __cdeclspec(...) is indeed Microsoft. Dont't know about other platforms.

You need to tell the compiler where to search for your libraries and includes. In both GCC and MinGW this is achieved by using -I for includes, and -L for libraries. I don't think its necessary for you to generate the .lib (or .a as it usually is in *nix and GCC), GCC should be able to find your function in the dll.

For example, build and link the shared lib:

g++ -shared -olibmy_dll.dll my_dll.cpp

Then, assuming all the files are in the same dir, build and link the exe and tell GCC to use your lib:

g++ -oapp.exe app.cpp -lmy_dll -L.

Note the difference between -l and -L , one tells GCC what libs to use, while the other tells where to look for them. In Eclipse CDT you find these settings in: Project properties -> C/C++ Build -> Settings -> MinGW C++ Linker -> Libraries.

Also, __declspec is recognized by MinGW, but I think its Microsoft specific anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM