Default flags for gcc compiler in Eclipse - c

I want all my C programs to be compiled with the options -Wall -pedantic -ansi by default. Is there a way to have Eclipse add these flags to the compiler command by default for all projects?

Assign CFLAGS to include those values, and have Eclipse run a tool that uses that environment variable by default when compiling (such as make).
You may have to specify environment variables before running Eclipse (and then they get inherited when Eclipse runs make) but there might be a way to specify default environment in Eclipse.
(I don't use Eclipse, so I'll have to see about installing and testing this; or maybe this answer can jog someone's memory, if so, feel free to edit.)
As an aside, you might want -std=c99 instead of -ansi. The -ansi option simply means -std=c89 or -std=c++98, depending on whether you're compiling C or C++, and both of those standards are showing their age.
I installed Eclipse inside a VM running Windows to test this, and, even though CFLAGS is in the environment, Eclipse doesn't use it. Eclipse also pretends (by displaying text like "make all" and "make clean") that it's running make in a few situations/projects I tried, when it is not really using make (probably using some internal engine). This answer was on the wrong track for Eclipse.

Assuming you are using Eclipse's internal builder goto Preferences->C/C++ Build->Settings
Choose the warnings section for the compiler, there are tick boxes for -Wall and -pedantic
For -ansi set in Miscellaneous
As the OP notes this is just for each project not a global setting

Eclipse on Windows: For a project: Properties -> C/C++ Build -> Setting than "Tool Setting" tab. select "CGG C++ Compiler" than at the right side you will see Command : g++
modify it to Command: g++ CFLAGS for instance if you like to have C++11 support modify as Command: g++ --std=c++11
PS: This modification will valid for only current project and for only current configuration. If you want it for all configurations modify each configuration (Run, Debug) similarly.

EDIT: I see that the OP runs Windows from a prior comment, however the following information may benefit users of Eclipse on the Linux platform, if Eclipse honors the alias.
Are you running Eclipse in Linux? If so, try aliasing the gcc command; run this at a terminal:
alias gcc='gcc -Wall -pedantic -ansi'
This is a common method in Linux to specify default parameters for an application. However, Eclipse might execute the actual gcc application and ignore the alias; I have not tested it.

Yes, Run as -> Run configuration -> 1st Tab is "Main" , choose the second tab(the one next to it) , you have there arguments box, paste -Wall -pedantic -ansi and just apply then run. Every next time you run you'll have these arguments as default

Related

How to set up OpenMP?

I am using Codeblocks and have to run an OpenMP C program. So, I added the flag -fopenmp in Codeblocks (compiler settings) and am now getting the error of 'mingw32-g++.exe: error: libgomp.spec: No such file or directory'
So after a bit of searching on the internet about the error, I downloaded TDM-GCC ( installed in C:\TDM-GCC-64). But still the same error is being shown on Codeblocks.
What am I doing wrong?
Here is the build log:
mingw32-gcc.exe -c "D:\Language Files\MatrixMultiplication.c" -o "D:\Language Files\MatrixMultiplication.o"
mingw32-g++.exe -o "D:\Language Files\MatrixMultiplication.exe" "D:\Language Files\MatrixMultiplication.o" -fopenmp
mingw32-g++.exe: error: libgomp.spec: No such file or directory
There are multiple possible causes for this: Either you did not install OpenMP with the compiler or you made a mistake in the Code::Blocks configuration. Anyways go through the following steps and you should be able to fix it. You seem to be working on Windows but I also added remarks on how to do it on Linux.
1) You will need a compiler that comes with OpenMP. For Windows download TDM-GCC preferably the 64-bit executable (second file) and install it. Make sure you select OpenMP in the component tab: Components > gcc (TDM current: ....) > OpenMP (the last entry). Linux already comes with GCC so just open the command line and get yourself OpenMP by typing sudo apt-get install libomp-dev in the terminal.
2) You will have to configure the Compiler in Code::Blocks now: Go to Settings > Compiler under Selected Compiler select GNU GCC Compiler and click Copy and type in a convenient name for your new compiler such as TDM-GCC Compiler. Go to Toolchain executables and browse the directory for all the Program files (C compiler, C++ compiler, Linkers, Debugger, Resource compiler, Make) you should be able to find them in C:\TDM-GCC-64\ ...\bin on a Windows machine if you chose the default installation. I can't tell you the precise sub-directory as I working on a Linux machine but you should be able to find it pretty easily. Depending on your installation there might be two folders for 32- and 64-bit. For Linux this step is not necessary.
4) Then you need to set the Linker settings. This can be done for all projects (which I would not recommend) by doing the following steps in the aforementioned menu or for your current project by clicking Project > Build options. Go to Linker settings of the corresponding configuration (Debug or Release) and click Add under Link libraries. On a Windows machine you will need to browse a file called libgomp-1.dll (32-bit) or libgomp_64-1.dll (64-bit) which should be located in the same folder as the aforementioned Program files. Under Linux instead choose -lgomp under Other linker options.
3) Set the compiler flag -fopenmp (for all the projects or only the current one) by going to Compiler settings > Other compiler options and typing in there -fopenmp.
4) Test it with a program like the OpenMP "Hello World".

difference of compiler between macOS and linux?

I am now learning c language, and my school put all assignments on myth, every time we have to log in by ssh and execute command remotely.
Thus I want to download the files and execute them on my own macbook. However when I use make command to compile the files, I got errors and warnings such as :
gcc -g -O0 -std=gnu99 -Wall $warnflags -m32 -c -I. vectest.c -o vectest.o
warning: unknown warning option '-Wlogical-op'; did you mean '-Wlong-long'?
vectest.c:10:10: fatal error: 'error.h' file not found
#include <error.h>”
I googled these problems but could not find a satisfactory answer. can anyone help me solve this ? or I have to use a linux machine instead?
Indeed; compilers for various platforms (even if it's the "same" compiler, such as GCC) may have different flags and behaviors. You may be able to get it to work - you could remove the -Wlogical-op flag from $warnflags in your Makefile, but if the error.h file is a system-supplied header file, you're probably in trouble. Therefore, I suggest that you download e.g. VirtualBox and run Linux on it.
See error(3) for what this header provides. It's not specific to linux but to the GNU C library. What you COULD do is provide your own minimal implementation of these functions and write your own error.h.
You could even `#define' them to do nothing at all, but then you will probably lose some error reporting in the existing code. Maybe you could try to find a teacher understanding the problem and discuss the issue ... it's probably better to learn standard C not using any platform-specific extensions.

Eclipse Looking for g++ Instead of gcc

I have the opposite issue compared to most questions I see posted here, and my google-fu has run out. I'm using Eclipse Luna to work on a C project (on Ubuntu 14.04), with a makefile that I am not allowed to modify. There is no C++ in this project.
The makefile uses the $(CC) variable instead of explicitly setting a compiler, and there is no CC= or CC?= set to anything within the makefile.
Running make in terminal works without issue to compile my project, however in Eclipse it says Program "g++" not found in PATH.
I set up eclipse to use make as the build command and set the toolchain to be "Cross GCC". I don't have a single c++ file in my project, so why would it be looking for g++ as the default compiler? Did I miss some detail to set this up, such as adding CC as a variable set to something within eclipse?
The issue was that the CC environment variable was never properly being set anywhere so Eclipse would default it to g++. In order to fix this follow the below steps:
Open the Project's Properties
Click on the C/C++ Build option on the left hand side
Uncheck "Use default build command"
Make sure you have a valid build command in the box usually just make will do unless you're using other options.
Click on the button named 'Variables..."
Locate CC and update the value to what it should be.
Here's the documentation where I located this: help.eclipse.org/luna
According to this forum post another option is to actually update the command being passed inside of that 'build command' box to make CC = gcc, however I've never tested this.

Set cc to gcc instead of clang on OSX Yosemite

I've been trying to get Mac OSX Yosemite to use gcc instead of clang when cc is invoked, but no matter what I do, it refuses to play along. I've already tried changing my bash_profile/bashrc and even relinking the symlink, but to no avail--every time I invoke "cc" it is still clang that runs. I'm trying to force it to be gcc instead (and no, just calling gcc is not an option).
I previously asked a similar question (Make gcc default c compiler on Yosemite/disable clang).
Neither OS X nor Xcode comes with real GCC. For compatibility with scripts which assume the compiler is called "gcc", it has executables by that name, but they are all fronts for Clang. No amount of symlinking, setting environment variables, or setting up aliases will allow those executables to run real GCC.
If you want GCC, you need to install it. You can do that using one of the package managers, such as MacPorts (the one I'm familiar with). I'm sure you could also use Homebrew.
You should not modify anything in /usr/bin. If you've already done so, you should restore what you've changed, if possible.
Well-behaved package managers will also not modify that directory. They should install to a separate directory such as /opt/local/bin, /usr/local/bin, or the like. In that case, you will want to modify your PATH to put those directories earlier than /usr/bin.
You shouldn't do this. A lot of tools rely on the installation binaries to point to what they should. Another option is to set the environment variable CC to gcc, and invoke compilation with $CC ... rather than cc ... ; this environment variable will also be picked up when building packages via configure (autotools) or cmake.
This is also a compatible approach with MacPorts versions of gcc (like the much more up to date gcc-4.9.2), which can be set up with: sudo port select --set gcc mp-gcc49

Eclipse can't run my Makefile

I'm writing a C project in Eclipse and while trying to run it I get the following error message:
(Cannot run program "make": Launching failed)
My Makefile is:
all : GenericHashTable.o TableErrorHandle.o
gcc -Wall GenericHashTable.o TableErrorHandle.o -o all
GenericHashTable.o : GenericHashTable.c GenericHashTable.h TableErrorHandle.h
gcc -Wall -c GenericHashTable.c -o GenericHashTable.o
TableErrorHandle.o : TableErrorHandle.c TableErrorHandle.h
gcc -Wall -c TableErrorHandle.c -o TableErrorHandle.o
clean :
rm all *.
Is the formatting broken in your makefile or in your question? Commands on the line below the target & dependencies. Does this makefile work from the command line?
Assuming the makefile is correct check the obvious things such as ensuring Eclipse can see your toolchain. Perhaps it can't even find the make command or you haven't set it from preferences.
Also the CDT offers a managed makefile and a standard (manual) makefile. The managed means Eclipse will create the makefile for you. The standard makefile is a makefile you are responsible for writing. Perhaps if your project is simple you should use the managed makefile to save yourself the hassle of writing one.
You can try the internal builder from eclipse:
Project->Properties->C/C++ Build
There (in the top level of C/C++ Build) you find Builder Settings->Builder Type which you set to Internal Builder. This way CDT does not require an external make command.
Either use the internal builder as "Turbo J" already suggested or make shure 'make' is in your PATH.
You can set the PATH for the build process in the Project-Properties in 'C/C++ Build -> Environment' - click "Select..", choose PATH and then change it by adding the correct path for the 'make' command.
This way you can also set the PATH of your compiler - that may be necessary if you use the Internal Builder.

Resources