Sublime Forum

C program compilation issues

#1

Hello,
I am experiencing problems when I try to build and run code written in C. I have Xcode installed. I am using Sublime Text 2. I have my file opened as C, but this is what I got in log when I built it:

clang: warning: treating ‘c’ input as ‘c++’ when in C++ mode, this behavior is deprecated
/Users/lukas/Desktop/algoritmy/heap.c:35:10: error: cannot initialize a variable of type 'int ’ with an rvalue of type 'void
int
newArray = malloc(size
sizeof(int));
^ ~~~~~~~~~~~~~~~~~~~~~~~~
/Users/lukas/Desktop/algoritmy/heap.c:43:10: error: cannot initialize a variable of type 'int ’ with an rvalue of type 'void
int
newArray = malloc(size
sizeof(int));
^ ~~~~~~~~~~~~~~~~~~~~~~~~
2 errors generated.
[Finished in 0.4s with exit code 1]

I have no issues with building the same code in Xcode, but in Sublime it seems to be a problem. If you have any idea how to fix this, please try to help me in some kind of foolproof way. Thanks.

0 Likes

#2

as the error message is saying you try to initialize a variable of type 'int *' with an rvalue of type 'void *' as malloc, beeing a C functions (so no C++ magic) returns a void* (read address of something, 4bytes value on x86, 8 on 64bit processors) which you try to assign to anint*. Here the trouble comes: in C casting ofvoid*to anything is handled automatically, in C++ you have to cast explicitly (using either one of thestatic,dynamic,reinterpret_cast<int*>(…), or the C-style cast(int*)…. As the clang warns you you are compiling C code as C++, which leads to the error. To fix it you need to be sure the correct command is issues when you hit compile, you might need to provide an own build system, but probably you better start using something likemakefileorcmake`

0 Likes