Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Error C2440 when using exisiting C source

I am having an issue where I have a C2440 error when compiling a C++ project in VS2005. The error is due some existing C code in another project which I depend on which casts a void pointer to either a char or int pointer.

The code resembles:

void * bbb;
... // some code which defines the void pointer
int * aaa = bbb;

However in C++ I need to specifically cast the type to be valid such as:

int * aaa = (int *)bbb;

My question is whether there exists a flag or compile option in VS2005 which allows my to compile my main project in C++ and ignore this error from depending projects I want to compile as C?

I would rather not change any of the original source as it is a shared project.

like image 673
Markus Avatar asked Jul 20 '26 17:07

Markus


2 Answers

If you name the file "something.c", the compiler will [unless explicitly told otherwise] compile it as C (and thus happily accept your pointer conversion without using a cast). A file named "something.cpp" will be compiled as C++, and you will need a cast to convert a pointer to a different type, even if it's a void pointer.

like image 94
Mats Petersson Avatar answered Jul 22 '26 09:07

Mats Petersson


The only way to do this was to edit the header files which caused the problem. It turned out to be a minor number of files which could be merged back into the shared projects.

There is no way to get around this in VisualC++. I had used -fpermissive to ignore the error in gcc.

So I just made the update to:

int * aaa = (int *)bbb;
like image 35
Markus Avatar answered Jul 22 '26 08:07

Markus