codegen matlab to c++ :- Problems when attempting to build a c++ based .exe rather than c?
-
01-06-2021 - |
Frage
I am having a few problems with using codegen (via the gui interface).
I have successfully built a very simple c based .exe program based on the following two files.
coderand.m
function r = coderand() %#codegen
r = rand();
main.c
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
int main()
{
printf("coderand=%g\n", coderand());
return 0;
}
If I now try and change out main.c for the same code in a main.cpp,
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
void main(int argc, char **argv)
{
printf("coderand=%g\n", coderand());
}
I get the following compile errors.
main.obj : error LNK2019: unresolved external symbol "double __cdecl coderand(void)" (?coderand@@YANXZ) referenced in function _main 25 F:\CoderTest\coderand.exe : fatal error LNK1120: 1 unresolved externals
Any help much appreciated.
Edit:- Solved by myself...
For those suffering the same problem...
Coder -> More Settings -> All Settings -> Advanced -> Language..change C to C++
Lösung
C++ can call C functions without difficulty, you just have to let the compiler know that the C calling convention applies to this function, like so:
extern "C" {
# include "coderand.h"
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow