how to export a class pointer with a name space in vs2015?

hitbuyi 46 Reputation points
2021-01-07T06:40:19.523+00:00

in module A

file1.c
namespace vehicle_control {
class FusionNodeRunnable {
int speed;
}
}

file2.h
extern vehicle_control::FusionNodeRunnable _declspec(dllexport) Fusion;
file2.cpp
vehicle_control::FusionNodeRunnable _declspec(dllexport) Fusion;

in module B

pFusion is accessed during pre-compling phase, but error occured during linking, it prompts

Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "class vehicle_control::FusionNodeRunnable fusion" (?fusion@@3VFusionNodeRunnable@vehicle_control@@A) Simulator_Fusion D:\WorkInHH\YCZL\imm_tracker\Simulator_Fusion_x64\Simulator_x64\Simulator_Fusion\simulator\simulator.obj 1

how to solve this problem?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,757 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
1,004 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Guido Franzke 2,196 Reputation points
    2021-01-07T07:38:49.543+00:00

    Hello,
    you must add the lib file of the dll to your project.
    Regards, Guido


  2. Viorel 118K Reputation points
    2021-01-07T08:05:18.62+00:00

    In my opinion, if module A is a Static Library, then __declspec(dllexport) is not needed.

    If module A is a DLL, then __declspec(dllexport) can be used, but it must be replaced with __declspec(dllimport) when file2.h is included into module B.

    This can be done with a macro. Try creating a new DLL using Visual Studio dialogs (Windows Desktop Wizard template) and select DLL type and “Export symbols” option. It will create a project with sample exported functions, data, and classes, and which uses special macros. Use Add Reference dialog in project B to add a reference to project A.

    Is A a DLL or Static Library?


  3. Jeanine Zhang-MSFT 9,771 Reputation points Microsoft Vendor
    2021-01-08T07:01:59.833+00:00

    Hi,

    According to the Doc:

    In C++ object files, Name decoration encodes the calling convention, class or namespace scope, and return and parameter types of a function. The encoded string becomes part of the final decorated function name. This name is used by the linker to resolve, or match, calls to the function from other object files. To fix this issue, make sure the function declaration, definition, and calls all use the same scopes, types, and calling conventions.

    In the header file you're declaring the variables in the namespace. I suggest you could declare them with the extern keyword and then in a source file you do the definition.

    Best Regards,

    Jeanine


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.