dylnmc
dylnmc

Reputation: 4020

Netbeans C GCC using C99

I went to Netbeans > Preferences > C/C++ > Build Tools and changed my C Comiler: from usr/bin/gcc to usr/bin/c99 so that I could have a local variable in a for loop declared when the for loop is first encountered.

I still get an error...

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/GNU-MacOSX/barchart
mkdir -p build/Debug/GNU-MacOSX
rm -f "build/Debug/GNU-MacOSX/main.o.d"
c99    -c -g -MMD -MP -MF "build/Debug/GNU-MacOSX/main.o.d" -o build/Debug/GNU-MacOSX/main.o main.c
c99: illegal option -- M
usage: c99 [-cEgs] [-D name[=value]] [-I directory] ... [-L directory] ...
       [-o outfile] [-O optlevel] [-U name]... [-W 64] operand ...
make[2]: *** [build/Debug/GNU-MacOSX/main.o] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 432ms)

I know my code isn't screwing up because it's just a simple for loop in the main:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {
    for (int i = 0; i < 42; i++) {
        printf("%d\n", i);
    }
    
    return (0);
}

I have read the questions on stack overflow where people have repeatedly asked how to do this. Most respond by saying "Add -std=c99 to your command line". Netbeans allows you to do this in the C Build Tools menu, but it didn't work for me. (Before that I tried to change my MakeFile, but that didn't work either). I finally tried to change my C interpreter as I stated above so that it points to the C99 C interpreter, but that didn't work either.

This shouldn't be so hard. Is there a solution that anyone has found that works (at least in Netbeans)?

Note:

I fixed this by using CLang (which I didn't even know I had). I don't know how well it performs in comparison to GCC, but I do know that it (hopefully) uses C99. It's still in the same target directory (/usr/bin/) so /usr/bin/CLang points to the CLang compiler... and /usr/bin/CLang++ is the one for - you guessed it - C++. If I have any problems with CLang, I'll probably post below so that people can determine if they want to use it, but at first glance... I can use a local variable in a for loop; yay!

From what I just read, however, GCC is compatible with more programming platforms (C/C++, java, etc), but CLang is faster and uses less memory: I got this from here

Upvotes: 0

Views: 1380

Answers (3)

ollo
ollo

Reputation: 25380

You can set the C Standard like this:

Open project properties -> Build -> C Compiler -> C Standard and set C99 there.

The C Standard should contain Standard, C89, C99 and C11 on a current NetBeans / compiler.

Don't change the compiler!

Upvotes: 1

dylnmc
dylnmc

Reputation: 4020

Sorry; I know I am answering my own question, but I think this is valid because I got it setup on Mac os x with Clang, but I could not replicate this on Windows, and I still could not get -std=c99 to work. So, here is my solution.

A .c file can run in any properly configured C++ interpreter. Therefore, in order to allow C99 implementation, you can simply make the path to the "C" compiler, the path to a C++ compiler.

For example, use g++ instead of gcc. If you are in Netbeans 8.0, simply go to the options (windows)/preferences (mac) > C/C++ > Build Tools and change the cc in gcc to ++.

Done. At least it worked for me.

If you're doing this though, you should probably just create a .cpp file.

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249582

Go to your Project Properties, then Build -> C Compiler -> Additional Options, and add -std=c99. Like this:

NetBeans Project Properties with -std=c99

Your compiler should remain as /usr/bin/gcc (hopefully you have a new enough version!).

Upvotes: 0

Related Questions