具有多个C和C++源文件的mkoctfile
mkoctfile with multiple C and C++ source files
我无法让mkoctfile
成功创建作为我的某些C++
函数(例如void my_fun(double*,double)
)包装器的oct
文件。特别是,我的问题源于这样一个事实,即包装代码my_fun_wrap.cpp
需要包含仅提供C++
标头的<octave/oct.h>
库(请参阅此处),但my_fun
的原始代码也使用C
中的源代码。例如
// my_fun_wrapper.cpp
#include <octave/oct.h>
#include "custom_functions_libc.h"
DEFUN_DLD(my_fun_wrapper,args, , "EI MF network model A with delays (Brunel, JCN 2000)"){
// Input arguments
NDArray xvar = args(0).array_value();
double x = xvar(0);
// Output arguments
double dy[4];
dim_vector dv (4,1);
NDArray dxvars(dv);
// Invoke my C function which also includes code in the lib file custom_functions_libc.c
my_fun(dy,x);
// Then assign output value to NDArray
for(int i=0;i<4;i++) dxvars(i) = dy[i];
// Cast output as octave_value as required by the octave guidelines
return octave_value (dxvars);
}
然后假设我的custom_functions_libc.h
和custom_functions_libc.c
文件位于文件夹<path_to_folder>/my_libs
中的某个位置。理想情况下,从Octave命令行,我将通过以下方式编译以上内容:
mkoctfile -g -v -O -I<path_to_folder>/my_libs <path_to_folder>/my_libs/custom_functions_libc.c my_fun_wrapper.cpp -output my_fun_wrapper -lm -lgsl -lgslcblas
这实际上根据需要生成my_fun_wrapper.oct
。然后我可以从一些octave
代码中调用后者,例如
...
...
xx = [0., 2.5, 1.];
yy = [1e-5, 0.1, 2.];
dxv = test_my_function(xx,yy);
function dy = test_my_function(xx,yy)
xx += yy**2;
dy = my_fun_wrapper(xx);
endfunction
事实证明,上面的代码将在test_my_function
中退出,并表示在my_fun_wrapper
中符号Zmy_fundd
未被识别。收到这样的错误后,我怀疑链接过程中出现了问题。但奇怪的是,编译器并没有像我说的那样产生任何错误。然而,对编译器的详细输出进行仔细检查后发现,mkoctfile
会根据扩展名在不同文件之间自动更改编译器。因此,my_fun_wrapper.cpp
由g++ -std=gnu++11
编译,而custom_function_libc.c
由gcc -std=gnu11
编译,并且在某种程度上,当与my_fun_wrapper.o
链接时,该编译过程所产生的custom_function_libc.o
文件与未解析符号不匹配。
上面的例子非常简单。在实践中,在我的案例中,custom_function_libc
包含更多的自定义C
库。到目前为止,一个解决方法是将这些库的.c
源文件克隆到.cpp
文件中。但我不太喜欢这种解决方案。
如何最终安全地混合C++
和C
代码,并通过mkoctfile
成功编译它?octave
手册建议准备一个extern C
规范(见此处),恐怕我不太熟悉。这是最好的方法吗?你能给我一个替代方案吗?
因此,根据我上面的帖子,显然最简单的解决方案是通过以下预处理器指令更正包装:
// my_fun_wrapper.cpp
#include <octave/oct.h>
// ADDED code to include the C source code
#ifdef __cplusplus
extern "C"
{
#endif
// END ADDITION
#include "custom_functions_libc.h"
// ADDED code to include the C source code
#ifdef __cplusplus
} /* end extern "C" */
#endif
// END ADDITION
...
...
这将编译和链接良好。
- 为测试目标创建具有不同源文件夹的文件
- 将图像添加到资源文件夹UWP C++
- 视觉工作室项目.提取源文件夹名称
- 生成一个生成文件,该生成文件使用Automake在一个步骤中编译和链接所有源文件
- make 命令如何避免重新编译未更改的源文件?
- C++两个源文件之间共享的枚举的静态实例
- 如何使用Qt 3D库加载和显示搅拌机.obj源文件场景
- 生成文件:动态源文件名和对象目录
- 无法使用 CMake 从其他文件夹添加源文件
- MinGW64 - Unicode 源文件错误?
- 有关从C++中的不同源文件引用类的问题
- "错误 (活动) E1696 无法打开源文件 "stdafx.h"--
- CMakeLists:使用 ExternalProject 从 Github 添加源文件
- 如何打印源文件中与 GDB 中的程序流关联的所有行号
- CMake 源文件找不到头文件
- 使用宏计算源文件行?
- 如何在 Eclipse 中一次从多个源文件进行构建
- 使用多个源文件时对类成员函数的未定义引用
- 给定一个源文件,有没有办法要求 gcc 返回仅直接包含的头文件的列表?
- 是否可以跨多个源文件构建 constexpr 数据结构?