0

I have a dll created using MSVC. The header file and a .def file are also present. Here is what is exported from the dll (simple.def):

EXPORTS
simple_initialize
simple_step
simple_terminate

In1
Gain
Increment

I have a simple application that is trying to access the functions and variables inside the dll (have the .lib file as well). The MSVC project files have the necessary files provide and the application builds correctly. Here's the application code:

#include <stddef.h>
#include <stdio.h>              
#include "simple.h"
#include "rtwtypes.h"
void main(void)
{
    int i;      
    simple_initialize();
    for (i = 0; i < 10; i++)
    {       
        simple_step();
    }
    simple_terminate();
}

This code seems to be working fine, however, if I try to write to any of the global variables (In1, Gain or Increment) from the dll it results in a crash.

After researching I realized that the only way I can make it work is by adding

__declspec(dllimport) real_T In1;

AND commenting out this line from the header file:

extern real_T In1;   

If I do not comment this line, then I get following error:

Error C2370 'In1': redefinition; different storage class SimpleTest \SimpleTest.c 8

  • So, what's your problem? Why can't you add the attribute in the header file, like: `extern __declspec(dllimport) real_T In1;`? Or do you have the problem of using the same header for both the DLL and the EXE? – Adrian Mole Aug 06 '21 at 16:06
  • So, the issue is: I want to ask the community as to why do I need to use the declspec to import the variables if they are already externed out via the header file. Am I missing anything? Should inclusion of the header file not suffice (like it did for the function calls). Notice that I do not need to use the __declspec import for the functions. Secondly, while this change seem small, it is an example code. This original code auto generated and will have many variables and commenting out the stuff from header files will be a difficult task (unless automated). Thank you. – Amit Deshpande Aug 06 '21 at 17:10

0 Answers0