我在使用 codegen 时遇到了一些问题(通过 gui 界面)。
我已经基于以下两个文件成功构建了一个非常简单的基于 c 的 .exe 程序。
coderand.m
function r = coderand() %#codegen
r = rand();
主.c
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
int main()
{
printf("coderand=%g\n", coderand());
return 0;
}
如果我现在尝试将 main.c 更改为 main.cpp 中的相同代码,
主要.cpp
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
void main(int argc, char **argv)
{
printf("coderand=%g\n", coderand());
}
我得到以下编译错误。
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
非常感谢任何帮助。
编辑:- 由我自己解决...
遇到同样问题的人...
编码器 -> 更多设置 -> 所有设置 -> 高级 -> 语言..将 C 更改为 C++
最佳答案
C++ 可以毫无困难地调用 C 函数,你只需要让编译器知道 C 调用约定适用于这个函数,就像这样:
extern "C" {
# include "coderand.h"
}
关于c++ - codegen matlab 到 c++ :- Problems when attempting to build a c++ based . exe 而不是 c?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178913/