c++ - codegen matlab 到 c++ :- Problems when attempting to build a c++ based . exe 而不是 c?

标签 c++ c matlab matlab-coder codegen

我在使用 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/

相关文章:

matlab - 如何在matlab中计算数学函数?

c++ - 将 nvcc 与 CUDA 6 和 VS 2012 Express 结合使用

c++ - 使用 MinGW 编译 SFML 项目

c++ - 在 C 中定义常量

c++ - 初始化类构造函数初始化列表中对象数组的第一个条目

c - unix 域套接字的 tcp_nodelay 设置

c++ - 将 Fortran 语言转换为 C 或 C++

matlab - 矩阵创建Octave/Matlab,无循环解决方案请求

将自删除推回 vector 时 C++ 崩溃

c++ - 初始值设定项是基类名称时出现错误 'initializer does not name a non-static data member or base class'