简体   繁体   中英

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" . 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. Note: C++ does not support defualt-int.

How i can deal with this?
I use MSVC2017 on MS-Windows10 .

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. How i can use it in my own C++ program?

All functions should specify a return type. You aren't specifying one for some .

In older versions of C, if you omit the return type of a function it defaults to int . C++ however doesn't support that.

You should always specify a function's return type:

int some(void)
{
    return 10;
}

extern "C" only changes the linkage of declarations. In particular, it disables C++ name mangling that is otherwise needed for some C++ features such as overloading.

extern "C" does not make the enclosed part of the program to be compiled as C. As such, the declarations must still be well-formed C++. some (void) is not a well-formed declaration in C++, which explains the error.

How i can deal with this?

Declare the return type explicitly.

USBPcap declare some function like that. How i can use it in my own C++ program?

Then you cannot use that header. You can write a C++ compatible header yourself. Or, you can use a C++ compiler that supports impilict int as an extension.

PS Implicit int is not well-formed in C language either since C99.

  1. Do not put any code to the.h files except static inline functions
  2. Secondly declare the functions correctly - not the lazy way. What is some(void) ? If the C++ compiler knew the return type of the function....
    extern "C"
    {
        int some (void)
        {
            return 10;
        }
    }
    #include <iostream>

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

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. 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. The definitions in C and C++ must have a type , if you want a function that not return anything, you need define it 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. So, finally, if you want write C or C++ code, please, add a type to your definitions, this isn't python or javascript...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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