0

Minimal reproducible example :

While creating the project I chose empty project in the menu. After creating project I did change configuration type to dll.

enter image description here

After this I created two files, Calculations.h and cpp

enter image description here

Content of both the files :

Calculations.h

#pragma once

#ifdef CALCULATIONS_EXPORTS
#define CALCULATIONS_API __declspec(dllexport)
#else
#define CALCULATIONS_API __declspec(dllimport)
#endif

CALCULATIONS_API int AddIntImpl(int a, int b);

Calculations.cpp

#include "Calculations.h"


int AddIntImpl(int a, int b)
{
    return a + b;
}

Now while building the same I am getting

1>Calculations.cpp
1>{...Path}\calculations.cpp(5): warning C4273: 'AddIntImpl': inconsistent dll linkage
1>{...Path}calculations.h(9): note: see previous definition of 'AddIntImpl'

How do I resolve the same?

Md Imran
  • 61
  • 1
  • 14
  • Did you add `CALCULATIONS_EXPORTS` as a preprocessor symbol so that `CALCULATIONS_API` is defined as `__declspec(dllexport)` when building the dll? You only want this defined when building the dll not using – drescherjm May 09 '21 at 16:23
  • No Sorry, how do I do that? – Md Imran May 09 '21 at 16:24
  • Related: [https://stackoverflow.com/questions/51830325/how-to-add-preprocessor-definition-on-visual-studio-2017-c](https://stackoverflow.com/questions/51830325/how-to-add-preprocessor-definition-on-visual-studio-2017-c) – drescherjm May 09 '21 at 16:27
  • This walkthrough assumes you use the dll project template: [https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-160](https://docs.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-160) I see the template adds the preprocessor definition in the settings for you. – drescherjm May 09 '21 at 16:31
  • I tried following this tutorial as well but I see files like dllmain, pch.h/cpp were created. Whats the difference If I do my way of creating empty project and not selecting dll? – Md Imran May 09 '21 at 16:37
  • One difference is the _EXPORTS preprocessor definition is not created for you automatically. However its easy to add yourself in `Project Properties -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions` make sure you add it for all configurations. – drescherjm May 09 '21 at 16:39
  • These dllmain have some entry point, pch includes some header files. I don't know why is it exactly required – Md Imran May 09 '21 at 16:41
  • It's required because `CALCULATIONS_API` needs to be defined as `__declspec(dllexport)` when building the dll and `__declspec(dllimport)` when not. This is one way to make that happen. – drescherjm May 09 '21 at 16:42
  • No Sorry, I was talking about other files generated when selecting the dll option like dllmain, pch, framework etc. What exactly they are doing if same can be done using empty project – Md Imran May 09 '21 at 16:43
  • You get your error because when building the dll you have `CALCULATIONS_API ` defined as `__declspec(dllimport)` which means that the definition is expected to be in some other dll but you define it in the dll you are building. And that is the reason for the inconsistent linkage. – drescherjm May 09 '21 at 16:43
  • The other files you can create if you need for example you don't have to use precompiled headers. – drescherjm May 09 '21 at 16:44

1 Answers1

0

Generally speaking, the warnings don't affect the running of an application. So warnings are usually be ignored when programming.

I suggest you could refer to the link:https://stackoverflow.com/a/19932564/11872808

And you could try the following ways to eliminate the warning:

1->Click Project->properties->configuration Properties->C/C++->Advanced->Disable Specific Warnings, and then add 4273 in the textbox.

2->Add the following sentence in your program: #pragma warning(disable:4273).

3->Turn off all warnings. In Property page, open C/C++ folder->General->Warning Level and select "Turn Off All Warnings (/W0)".

Jeaninez - MSFT
  • 2,068
  • 1
  • 3
  • 12
  • @Farhan Ahmed Have you got any updates? If your case has been solved, please help to mark answers. If not, just feel free to contact us. Your understanding and cooperation will be grateful. – Jeaninez - MSFT May 27 '21 at 08:45
  • You did not even answer, what he wanted, then how do you expect him to mark this as "answer" – Himanshu Poddar Oct 18 '21 at 06:22