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 ?

4
  • 3
    Your C code is not valid C99, which the MSVC2017 compiler expects. Commented Nov 10, 2019 at 15:37
  • 2
    some (void) is missing a return type. Commented Nov 10, 2019 at 15:38
  • isn't the error self explaining? Program the correct way - do not save the keyboard. Commented Nov 10, 2019 at 15:41
  • 3
    If that library declared functions like that - file a bug with the project to have that corrected. Or better yet - file a Pull Request that solves it for them. Commented Nov 10, 2019 at 15:47

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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"
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;
}

3 Comments

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++.
@GhasemRamezani Then you should direct your question to the library author on the project's github page.
@GhasemRamezani Is this code abandonware? The "implicit int" function return type was removed from C in C99 - literally twenty years ago.
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.