dll library

Hi

I have a third party visual studio project that seems like it produces DLLs. It has a header and cpp file that do dll export.

I want to include this project within my own solution containing my own projects.

But when I do, and when I  try to compile my visual studio solutin, I  get:

error C2491: 'funcA' : definition of dllimport function not allowed, pointing to this third party .cpp file

A.cpp
-------------
A_DLL
int funcA()
{
}


A.h
--------
#ifndef A_DLL
    #ifdef _WIN32
        #ifdef A_STATIC_LIB
            #define A_DLL
        #else
        #ifdef BUILD_DLL
            #define A_DLL __declspec(dllexport)
        #else
            #define A_DLL __declspec(dllimport)
        #endif
        #endif
    #else
        #define A_DLL
    #endif
#endif


How do i fix this error? To me it suggests that somehow the DLL, called a32.dll which exists in the A/lib folder is not being seen. But I have mentioned the A/lib folder in the Additional include directories under Project->Properties->C/C++->General->Additional Include Directories.

how does one normally import in from the dll anyway? What needs to be done? I am using visual studio 2008

thanks
LuckyLucksAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
If I get that correctly, you are trying to include that code into your own project. If so, you will either have to modify it to suit your needs or build the DLL separately and use that in your project, which might be the easier way if that project consists of multiple source files. But, if it is only one as the above suggests, replace

#ifndef A_DLL
    #ifdef _WIN32
        #ifdef A_STATIC_LIB
            #define A_DLL
        #else
        #ifdef BUILD_DLL
            #define A_DLL __declspec(dllexport)
        #else
            #define A_DLL __declspec(dllimport)
        #endif
        #endif
    #else
        #define A_DLL
    #endif
#endif

Open in new window

with just
#define A_DLL

Open in new window

and you should get rid of that error.
0
jkrCommented:
BTW, if you decide to use the DLL from the other project: Open the project in Visual Studio and build it. This will create the DLL and a .lib file of the same name. Add that .lib file (the so called "import library") to your project (either by right clicking on your project in the "Solution" tree view or your project's properties under "Linker|Additional libraries" and copy the DLL to your executable's directory to use it and rebuild your project as well.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ZoppoCommented:
Hi LuckyLucks,

following the info about the error C2491 (https://msdn.microsoft.com/en-us/library/62688esh.aspx) it seems the problem is using the __declspec(dllimport) is only allowed with a function declaration, not with it's definition.

IMO you should change is so the function is declared in the header using __declspec(dllimport) but defined in the cpp file like a 'normal' function, i.e.:
// in a.h after the #define A_DLL ...
A_DLL int funcA();

// in a.cpp after #include "a.h"
int funcA()
{
 ...
}

Open in new window

Hope that helps,

ZOPPO
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

sarabandeCommented:
the header file has a switch such that it can be used in the dll project as well as from any other project.

the difference is that the dll project wants to export the functions while other projects want to import the functions.

because of that the switch would activate the corresponding export or import specifier.

to use the header in the intended way, you have to either define or not define the macro BUILD_DLL.  

if you want to build the dll project yourself  (and export the functions) the macro BUILD_DLL should be added to the preprocessor macros (properties - configuration properties - c/c++ -  preprocessor - preprocessor definitions).

if you want to import the dll rather than build it, you have to remove the third party cpp from your project and use the .lib file (see comment of jkr). the .lib file already should be available and there should be no need to build the dll yourself.

if you want to use the code in your project rather than use the dll (or static library), I would suggest to use a new cpp and h file where all the dll and library stuff was removed from code and where you only overtake the needed functions and adopt them to your needs.

Sara
0
ZoppoCommented:
Hm - I suspect the code you posted still won't work only following jkr's suggestion without even changing the code as I wrote in my comment. If so it would have been better to mark my comment as 'assisted' ...
0
sarabandeCommented:
Zoppo,

jkr's suggestion to replace all the macro stuff in a.h by

#define A_DLL

is sufficient when the a.cpp source was added to the project without using or building a dll or lib.

Sara
0
ZoppoCommented:
Hi sara,

yes, I understood this, but IMO it's sure the error C2491 was caused by using the A_DLL macro at the function implementation as in the original posted code
A_DLL
int funcA()
{
}

Open in new window

For anyone else who searches for solution to fix a C2491 IMO it would be good to see the comment about this a bit more highlighted - and I think you know me good enough to know I don't care about the points here ;o)
0
sarabandeCommented:
of course not.

it surely would be better to not using the A_DLL macro in the cpp file at all rather than defining an empty macro in the header. but in both cases the error C2491 would be solved.

Sara
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.