Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have been trying to link the next code:

C++
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API __declspec(dllimport)
#endif

namespace MathFuncs
{
	 class MyMathFuncs
	 {
	 public:
		  static MATHFUNCSDLL_API double Add(double a, double b);
		  static MATHFUNCSDLL_API double Substract(double a, double b);
		  static MATHFUNCSDLL_API double Multiplty(double a, double b);
		  static MATHFUNCSDLL_API double Divide(double a, double b);
	 };
}


Which builds fine.

The problem occurs when I try to compile the next code:

C++
//Global scope
static MATHFUNCSDLL_API double Add(double a, double b);
static MATHFUNCSDLL_API double Substract(double a, double b);
static MATHFUNCSDLL_API double Multiplty(double a, double b);
static MATHFUNCSDLL_API double Divide(double a, double b);

As you can see, it is outside the class and belongs to the global scope. What it seemed to "work", was to change "static" by "extern". Nonetheless, this didn't work either.

And gives me the next error: Unresolved external symbol...

What should I do if I want to build this project?

Thanks in advance.
Posted

The global scope solution shouldn't work. Forget it. You have to include the same header you've shown in your first code block. The only difference between compiling your DLL and the other module that uses the DLL is that you have to compile all .c and .cpp files in your DLL project by specifying the MATHFUNCSDLL_EXPORTS define. You can define this symbol in visual studio by right clicking your DLL project options, "C/C++/Preprocessor/Preprocessor definitions". There list MATHFUNCSDLL_EXPORTS by separating it from the other already defined symbols with a semicolon. Don't forget to do this for all platforms (Win32/x64) and all configurations (debug/release/...)! This way the MATHFUNCSDLL_API will be expanded to __declspec(dllexport) in your DLL project while in all other cases in the modules that use your DLL and its header file will see MATHFUNCSDLL_API as __declspec(dllimport).

EDIT: Also make sure that you are compile both the exe and the dll with the same configuration (both compiled as debug or both compiled as release) before running! Don't try to run modules with mixed configs, without extern "C" export the C++ symbol name mangling of visual studio results in different names in case of debug and release and this is not only an accident!
 
Share this answer
 
v3
Comments
unscathed18 11-Aug-13 12:18pm    
Ok, but what is the reason it doesnt work?
pasztorpisti 11-Aug-13 12:19pm    
You made a mistake we don't see here.
File mathfuncs.cpp

C++
#include "precompiledheaderspec.h" // stdafx.h or whatever...

#define MATHFUNCS_EXPORTS  // (must be before all non-pch includes)
#include "mathfuncs.h"

#include "anything else you need..."
 
namespace MathFuncs
{
    static double MyMathFuncs::Add(double a, double b) { ... }
    static double MyMathFuncs::Substract(double a, double b) { ... }
    static double MyMathFuncs::Multiplty(double a, double b) { ... }
    static double MyMathFuncs::Divide(double a, double b) { ... }
}


File mathfuncs.h

C++
#ifdef MATHFUNCSDLL_EXPORTS
#define MATHFUNCSDLL_API __declspec(dllexport)
#else
#define MATHFUNCSDLL_API // (don't define anything)  __declspec(dllimport)
#endif
 
namespace MathFuncs
{
    class MATHFUNCSDLL_API MyMathFuncs
    {
    public:
        static MATHFUNCSDLL_API double Add(double a, double b);
        static MATHFUNCSDLL_API double Substract(double a, double b);
        static MATHFUNCSDLL_API double Multiplty(double a, double b);
        static MATHFUNCSDLL_API double Divide(double a, double b);
    };
}


Note the following:
- Add MATHFUNCSDLL_API to the class definition
- The MATHFUNCSDLL_API stuff should only be in the .h file (don't repeat in the .cpp file)
- remove the __declspec(dllimport) from the conditional define
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900