codegen:- Problems when attempting to build a c++ based .exe rather than c?

조회 수: 3 (최근 30일)
Adam Hartshorne
Adam Hartshorne 2012년 4월 16일
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.
.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
I assume it is because I am still building c code. What option do I have to change to fix this?

답변 (2개)

Kaustubha Govind
Kaustubha Govind 2012년 4월 16일
You probably need to add a extern "C" { } block around the declaration for coderand() in coderand.h, so that the C++ compiler doesn't use C++ style linkage.
Or try:
extern "C" {
#include "coderand.h"
}

Adam Hartshorne
Adam Hartshorne 2012년 4월 16일
I have found the solution...As suspected it was due to the codegen being set to build c rather than c++ code.
For those suffering a similar problem, you have to go to
Coder -> More Settings -> All Settings -> Advanced -> Language and change C to C++

카테고리

Help CenterFile Exchange에서 MATLAB Algorithm Acceleration에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by