My Situation is as follows: I have a C# .NET Application and need to use an API which is only available for C++. I thought I could create a DLL which contains some C++ code I can use to interface with the API. I then wanted to use functions from that DLL in my C# program. So far I have managed to create the C# program and some C++ code to test the API.
Now I have started creating the DLL (I am using a "C++ Dynamic Link Library (DLL) project in Visual Studio 2019 Community). First I just wanted to create a basic "Hello World"-kind of function to test if I can use the DLL functions in C#. I can call functions from the DLL in my C# program, but when creating the functions in the DLL I noticed something strange:
#include <stdio.h>
extern "C" __declspec(dllexport) void helloWorld(void){
printf("Hello World from DLL\r\n");
}
The above code wouldn't compile (error C3861 "printf": Bezeichner nicht gefunden. [this roughly translates to "printf": identifier not found.] I then tried basically the same Thing, but this time using cout:
#include <iostream>
extern "C" __declspec(dllexport) void helloWorld(void){
std::cout<<"Hello World from DLL\r\n";
}
the second Version does compile an I can call that function from my C# Program.
The Question to me is: Why?
The Problem is: the functions from the API I mentioned earlier have the same Problem as the call to printf. I have tried to search for the Problem, but I don't really know what to search for. Can any of you tell my where my error is? Any ideas how to solve the Problem, or better yet: an Explanation why this is a Problem in the first place, would be greatly appreciated.
EDIT: In the thread GSerg posted I found a solution: there was a #include "pch.h" I overlooked in my Code. That include Statement apparently has to be the first include Statement in the file. Rearranging the include Statements fixed the issue.