我们在平原开发了一些项目C(C99).但是,我们有一个库作为源代码(数学库)C++.我们需要这个库,所以我想问一下,集成这些源代码的最优雅方式是什么?
大小之比C,并C++正在20:1使移动C++是不是选项.我们应该使用静态库吗?DLL?(这都是在Windows上).
我是C和C++的新手.但我有一些C++函数,我需要从C调用它们.我做了一个我需要做的例子
main.c:
#include "example.h"
#include <stdio.h>
int main(){
helloWorld();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
example.h:
#ifndef HEADER_FILE
#define HEADER_FILE
#ifdef __cplusplus
extern "C" {
#endif
void helloWorld();
#ifdef __cplusplus
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
example.cpp:
#include <iostream.h>
void helloWorld(){
printf("hello from CPP");
}
Run Code Online (Sandbox Code Playgroud)
它只是不起作用.我仍然收到未定义参考错误_helloWorld在我main.c.这个问题在哪里?
我已经阅读了几种结合C和C++代码的方法,但是,我仍然对如何处理我的情况感到困惑.这是我的问题:
我有一个比较大的量的C语言代码(包括各种.c和.h文件),其用于建模在有限和离散元件的固体.该代码具有相对简短的主函数,其中for循环中顺序调用各种其他函数(来自其他文件).在Unix(icc编译器)和Visual Studio中编译时,此代码工作正常.
我在C++中有其他代码可以解决分子动力学相互作用.此代码还包含各种文件,并在Unix(icpc编译器)和VS中运行良好.两者都是独立程序,具有自己的输入和输出文件集.
我需要做的是以我的C程序在其主循环中"调用"C++代码的方式运行这两个程序.一些信息需要在两个代码之间双向传递,这两个代码可以是数组(或指针)的形式.
最简单的方法是什么?
特别是,根据我读过的建议,我有多个问题:
extern "C" {}吗?extern "C"在我的C函数中使用吗?extern "C"在我的C++文件中使用?(标题?函数?所有这些?还是只需要从C程序调用的那些?)main功能.我可以简单地重命名我的C++ main函数吗?main函数从C 转换为C++吗?main在C++中重命名并由我的C代码"调用";第五,实现传输信息?)抱歉,长文和多个问题.我对C相对较新,甚至比C++更新,所以即使我对这些程序的词汇量有限.
谢谢您的帮助.任何提示将不胜感激.如果您需要其他信息,请告诉我们.
这是我的C代码的"主要"功能:
#include "Yproto.h"
void show_time_info(YDC ydc,CHR Ystage[3]);
main(argc, argv)
INT argc; char **argv;
{ CHR c1name[300]; /* name of the problem i.e. input file */
struct YD_struct yd; /* Y database */
YDC ydc=&(yd.ydc); /* Y …Run Code Online (Sandbox Code Playgroud) 在C程序中,我需要获得一个位于用C++编写的库中的函数的引用.
这是代码的一部分
// Some C includes
#include <cpplib.h>
.
.
.
// A C structure attribute pointing to the C++ function
infoptr.EXT_meshAdapt = &meshAdapt;
Run Code Online (Sandbox Code Playgroud)
问题是编译器告诉我有一个未定义的引用meshAdapt,而当我对C库执行相同操作时没有问题.
在这个线程中,我看到我们可以通过创建一个包装器来调用C中的C++函数.但有没有办法在不制作包装器的情况下引用C++函数?
我有一个用 C 编写的程序,想在其中包含 gRPC。但是,gRPC 的 API 是用 C++ 编写的。
我看过here并且让foo_client和foo_server正常工作。 https://github.com/Juniper/grpc-c/tree/master/examples
但是,C 客户端与我的 gRPC C++ 服务器不兼容。他们不会互相交谈。我相信这是因为我使用的是使用 protocbuf 3.2.0 版的 lates gRPC。而瞻博网络的 grpc-c 使用的是旧版本的 gRPC,该版本使用 protocbuf 3.0.0 版。
因此,C 中的瞻博网络版本似乎不适用于新的 gRPC。我知道 gRPC 低级 C API 应该在这里:https : //github.com/grpc/grpc/blob/master/include/grpc/grpc.h 但我很难实现它。任何人都可以帮助我理解它吗?
我有一段时间没有用 C 编程,所以我有点生疏。
我必须使用第三方C++库(我无法更改)并从C代码调用其API.
对于大多数库API,我使用了一个包装器,如本文所述: 如何从C调用C++函数?
但是有一个API需要可变数量的参数.
这是它的定义(来自库提供的头文件):
void consoleDebug(char* format, ...);
Run Code Online (Sandbox Code Playgroud)
我不知道如何为这个API编写包装函数.
我试过这个,但它不起作用:
extern "C" {
void wrapper_consoleDebug(char * format, ...)
{
va_list argptr;
va_start(argptr,format);
consoleDebug(format, argptr);
va_end(argptr);
}
}
Run Code Online (Sandbox Code Playgroud)
欢迎任何想法!谢谢!
我有一个标题声明函数,这些函数将指向C++对象的指针作为参数.该实现是一个单独的C++文件.如何在C中包含此标头并使用C中的函数,即使参数需要是C++对象指针?
我正在编写一个需要使用特定 api 的 C 程序(myapp);api 是用 C++ 编写的。我使用过 C 和 C++,但从来没有同时使用过,而且我很困惑。
因此,api 提供了以下目录,我将其放置在名为 include 的文件夹中,与我的 makefile 处于同一级别:
libmyapi.a
api/api.h
Run Code Online (Sandbox Code Playgroud)
我的主要源文件是 src/myapp.c,它包含使用#include "api/api.h".
我的 make 命令是(加上一些标志,我没有列出,因为我认为它们在这里不相关):
gcc -Linclude -lmyapi -Iinclude src/myapp.c -o lib/myapp.sp -lrt
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是 api.h 文件包含对命名空间等的引用。例如,在某一时刻它具有:
namespace MyAPI {
namespace API {
typedef SimpleProxyServer SimpleConnection;
}
}
Run Code Online (Sandbox Code Playgroud)
显然 C 编译器不知道这意味着什么。
所以,我假设我需要使用 C++ 编译器进行编译,但后来有人说我没有,我可以将代码“包装”在“extern 'C'”中,但我真的不明白。在线阅读后,我不再继续。
我是否需要用 C++ 编译(即使用 g++)?
我是否需要“包装”代码,这意味着什么?我只是做
#ifdef __cplusplus
extern "C" {
namespace MyAPI {
namespace API {
typedef SimpleProxyServer SimpleConnection;
}
}
}
#endif
Run Code Online (Sandbox Code Playgroud)
还是我只是换行 …
我在MS Visual Studio 2010项目中混合使用C/C++代码库,并尝试从C src文件调用C++文件中定义的静态函数.现在我通过将C src重命名为CPP来实现它(ac - > a.cpp)只是想知道是否有一种更优雅的方式绕过它(转换一些神奇的编译器标志等)而不进行任何大规模的手术代码库(比如使用此线程中建议的不透明指针)
请注意我的代码库非常复杂,我已经创建了这个小的VS片段,用最小的可证明的代码重现错误
#include "b.h"
void test()
{
B::func();
}
Run Code Online (Sandbox Code Playgroud)
#ifdef __cplusplus
extern "C" {
#endif
class B{
public:
static void func();
};
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
#include "b.h"
#ifdef __cplusplus
extern "C" {
#endif
void B::func()
{
return;
}
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
错误: - 在MS Visual Studio 2010中
1>c:\.....\b.h(5): error C2061: syntax error : identifier 'B'
1>c:\.....\b.h(5): error C2059: syntax error : ';'
1>c:\.....\b.h(5): error C2449: …Run Code Online (Sandbox Code Playgroud) 我想使用vega库来处理 dicom 文件。其网站的示例代码如下:
#include <string>
#include "vega/dictionary/dictionary.h"
#include "vega/dicom/file.h"
int main() {
// Set the dictionary file
vega::dictionary::Dictionary::set_dictionary("/path/to/dictionary/dictionary.txt");
// Read the DICOM file in
const std::string file_name = "/path/to/dicom/file/dicom.dcm";
vega::dicom::File file(file_name);
// Print a human-friendly representation of the file to std::cout
vega::Formatter formatter(std::cout);
file.data_set()->log(formatter);
}
Run Code Online (Sandbox Code Playgroud)
本页解释了包括 C 代码,但是 C++ 代码呢?
该官方页面指出“如果该库是用C++编写的,则无法将其绑定到Vala,除非有C++库的单独C绑定(例如,LLVM)。”。因此,在我看来,我不能使用 vega 库。我对么?
编辑:另外,valabind / valabind-cc与swig有帮助吗?
我们正在尝试修改一些现有的C项目,即我们正在尝试从C代码中进行一些C++函数调用.我们尝试将编译器从gcc更改为g ++,但由于不兼容性,存在多个编译错误.我们要做的是在C代码中调用一些C++函数,而不对现有代码进行任何更改.简单地改变编译器似乎没有做到这一点.因此,我们试过以下:
#include <stdio.h>
extern "C"
{
int func(int new ) {
printf("in new func()\n");
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用命令编译它
g ++ -c hello.c -o hello
我们得到以下错误
hello.c:9: error:expected ‘,’ or ‘...’ before ‘new’.
Run Code Online (Sandbox Code Playgroud)
现在我们知道new是c ++关键字.如前所述,我们试图不对现有的C代码进行任何修改.有什么建议 ?
这些不是唯一的错误.还有与结构声明相关的其他错误.
attr.c:75:错误:在'.'之前预期的primary-expression 代币
在attr.c,第75行是
static post_op_attr error_attr = {.attributes_follow = FALSE};
问题是在代码中还有其他C样式的结构声明和初始化,所以即使我们重命名变量名,我们仍然需要修改C程序的其他部分.因此,我们正在寻找一种方法来在不修改现有C代码的情况下向C代码添加C++函数调用.