4

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 ?

Ghasem Ramezani
  • 2,683
  • 1
  • 13
  • 32

4 Answers4

5

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;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Yes thank you, I know that. but I use a library which is use default-int function type and i don't what to change the library source code and i what to use it in my own program which is C++. – Ghasem Ramezani Nov 10 '19 at 15:42
  • 1
    @GhasemRamezani Then you should direct your question to the library author on the project's github page. – dbush Nov 10 '19 at 15:52
  • 1
    @GhasemRamezani Is this code [abandonware](https://en.wikipedia.org/wiki/Abandonware)? The "implicit `int`" function return type was removed from C in C99 - literally twenty years ago. – Andrew Henle Nov 10 '19 at 20:28
5

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.

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

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • It is rather pointless of writing the code in the `extern "C"`. If the code is there, then there is no need of `extern "C"` – 0___________ Nov 10 '19 at 22:16
2
  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

0___________
  • 60,014
  • 4
  • 34
  • 74
0

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...

rez_23
  • 3
  • 1
  • 4