Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with using C code in C++ with extern "C"

Tags:

c++

c

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 ?

like image 543
Ghasem Ramezani Avatar asked Dec 18 '22 15:12

Ghasem Ramezani


1 Answers

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;
}
like image 104
dbush Avatar answered Dec 31 '22 00:12

dbush