简体   繁体   English

在带有外部“C”的 C++ 中使用 C 代码时出现问题

[英]Problem with using C code in C++ with extern “C”

I know when i want to link C code as C code in C++ should i use extern "C" .我知道当我想在 C++ 中将 C 代码链接为 C 代码时,我应该使用extern "C" But with the following code:但是使用以下代码:

/* file.h */
some (void)
{
    return 10;
}
extern "C"
{
    #include "file.h"
}
#include <iostream>

int main (void)
{
    std::cout << some() << std::endl;
}

I get this compile time error:我得到这个编译时错误:

C4430: missing type specifier - int assumed. C4430:缺少类型说明符 - 假定为 int。 Note: C++ does not support defualt-int.注意:C++ 不支持默认整数。

How i can deal with this?我该如何处理?
I use MSVC2017 on MS-Windows10 .我在MS-Windows10上使用MSVC2017

EDIT : I know that most declare the function with a explicit return type, But i what to use USBPcap and USBPcap declare some function like that.编辑:我知道大多数声明 function 具有显式返回类型,但我使用USBPcap和 USBPcap 声明一些 function 之类的。 How i can use it in my own C++ program?我如何在我自己的 C++ 程序中使用它?

All functions should specify a return type.所有函数都应指定返回类型。 You aren't specifying one for some .您没有为some指定一个。

In older versions of C, if you omit the return type of a function it defaults to int .在旧版本的 C 中,如果省略 function 的返回类型,则默认为int C++ however doesn't support that. C++ 但是不支持。

You should always specify a function's return type:您应该始终指定函数的返回类型:

int some(void)
{
    return 10;
}

extern "C" only changes the linkage of declarations. extern "C"仅更改声明的链接。 In particular, it disables C++ name mangling that is otherwise needed for some C++ features such as overloading.特别是,它禁用了 C++ 功能(例如重载)需要的 C++ 名称修改。

extern "C" does not make the enclosed part of the program to be compiled as C. extern "C"不会使程序的封闭部分编译为 C。 As such, the declarations must still be well-formed C++.因此,声明仍然必须是格式良好的 C++。 some (void) is not a well-formed declaration in C++, which explains the error. some (void)不是 C++ 中格式正确的声明,它解释了错误。

How i can deal with this?我该如何处理?

Declare the return type explicitly.显式声明返回类型。

USBPcap declare some function like that. USBPcap 像这样声明一些 function。 How i can use it in my own C++ program?我如何在我自己的 C++ 程序中使用它?

Then you cannot use that header.那么你不能使用那个 header。 You can write a C++ compatible header yourself.你可以自己写一个C++兼容header。 Or, you can use a C++ compiler that supports impilict int as an extension.或者,您可以使用支持隐式 int 作为扩展的 C++ 编译器。

PS Implicit int is not well-formed in C language either since C99. PS 隐式 int 从 C99 开始在 C 语言中也不是格式正确的。

  1. Do not put any code to the.h files except static inline functions除了 static 内联函数之外,不要将任何代码放入 .h 文件
  2. Secondly declare the functions correctly - not the lazy way.其次正确地声明函数 - 而不是懒惰的方式。 What is some(void) ?什么是some(void) If the C++ compiler knew the return type of the function....如果 C++ 编译器知道 function 的返回类型......
    extern "C"
    {
        int some (void)
        {
            return 10;
        }
    }
    #include <iostream>

    int main (void)
    {
        std::cout << some() << std::endl;
    }

https://godbolt.org/z/_AJgFX https://godbolt.org/z/_AJgFX

The compiler have say to you exactly what is the problem.编译器已经告诉你到底是什么问题。 The function that you are trying to call from std::cout need to have a returned type explicitly defined.您尝试从std::cout调用的 function 需要明确定义返回类型。 In C the compiler automatically set the return type of an undefined-type function to int and you can compile a code that contain untyped function with nothing more of a warning from the compiler, but not do it, it is extremely wrong way to write code. In C the compiler automatically set the return type of an undefined-type function to int and you can compile a code that contain untyped function with nothing more of a warning from the compiler, but not do it, it is extremely wrong way to write code . In C++, just like the compiler told you, you can't, so it is an error.在 C++ 中,就像编译器告诉你的那样,你不能,所以这是一个错误。 The definitions in C and C++ must have a type , if you want a function that not return anything, you need define it void . C 和 C++ 中的定义必须有一个 type ,如果你想要一个不返回任何东西的 function ,你需要定义它void However, in the last versions of the c++ standards you can use the auto keyword to auto detect the type of a declaration at compile time, so if you want that the compiler auto detect the type of something, use it, but use it with sparingly.但是,在 c++ 标准的最新版本中,您可以使用auto关键字在编译时自动检测声明的类型,因此如果您希望编译器自动检测某物的类型,请使用它,但要谨慎使用. So, finally, if you want write C or C++ code, please, add a type to your definitions, this isn't python or javascript...所以,最后,如果你想写 C 或 C++ 代码,请在你的定义中添加一个类型,这不是 Z23EEEB4347BDD26BFC6B7EE9A3B755DDEDE2911ZDE9B9ED780DEF7...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM