Automating GCC Compiler arguments to create easier compilations - Windows OS? - gcc

Goal
When I run the command:
gcc -ggdb -std=c99 -Wall -Werror hello.c -lcs50 -o test.exe from the root directory
I am able to build the test.exe file and when I run test.exe all is well (thanks to this post by Manohar Reddy Poreddy)
However all of those flags are a little bit cumbersome and I think it would great to condense them into a 'make' command or similar. How would I do this on windows?
Context
GCC, G++ and GDB all seem to be correctly linked (I used chocolatey which paths everything automatically)

Okay so I found what I was looking for.
I hope this answer can help others. Turns out the utility is called 'make' (no surprises). In your directory you essentially create a 'makefile' where you can include your command line arguments which saves on repeated typing in the command line for each compile.
Here is an excellent response on how to install 'make' for windows and was perfect for my use case as a Chocolatey user.
I also found this resource which helps newcomers begin to get their head round GCC which I highly recommend if you're coming into this like I was and felt completely out of your depth.

Related

Makefile executes only first part of command

I would appreciate if anyone could explain the following behavior to me
(Please do keep in mind I am COMPLETELY new in Makefile creation and I'm probably totally overlooking something. I'm only trying to use it to automate a compilation process)
client2:
g++ client.cpp -o client.app --std=c++11 -I/usr/local/include -lTgBot -lboost_system -lboost_iostreams -lssl -lcrypto -lpthread
(tab is inserted before "g++")
I am using the line above to execute a g++ command. However only this part of the command is executed when I use "make client2":
:~/**some-location-anything-anywhere**$ make client2
g++ client2.cpp -o client2
I'm confused as to why the rest of the g++ command is ignored and thus my compilation process completely fails (the command does work when I manually enter it into a shell). Am I missing out on something?
Any help would be appreciated! Thank you in advance.
Thank you to the ones who commented.
I had the issue named "make" instead of "Makefile", dummy error!
I got it fixed by renaming it to Makefile instead..

Can a CMD batch file or PowerShell script execute multiple g++ commands?

I like using both Linux and Windows for my C and C++ coding and I prefer using the command line to compile my programs. I can run make on Linux, which is fine. But on Windows, now that I'm working with classes and have to compile multiple files, I find it a chore to type in several g++ commands to compile the class and main object files.
I was wondering if there's a way to get a CMD batch file or PowerShell script to just execute the commands one after the other?
Something like this:
g++ -c Area.cpp -o Area.o
g++ -c Convert.cpp -o Convert.o
g++ -c Calculate.cpp -o Calculate.o
g++ -c multi_menu_functions.cpp -o multi_menu_functions.o
g++ -c main.cpp -o main.o
g++ -Wall main.o Area.o Calculate.o Convert.o multi_menu_functions.o -o main
...Something dead simple and easy.
Just write the commands in a file with extension .bat and you can just start that file. You can turn off outputting the commands while execution of the batch file by starting the file with the line #echo off.
Or better yet: Just get make for windows and use that one.
I figured out the issue of why the g++ commands wouldn't work as is: somehow the laptop I was using didn't grant me the correct permissions. At a guess I tried the full path name for g++.exe and it worked. I reconfigured some things and now it works with the commands as listed.
On a side note; I did get gnumake and minGW make working as well. Since these can run my Linux makefiles I'll use these as well.

How can I specify include and library locations for mingw32-make under msys2?

I have a suite of Windows programs that up to now I have built under msys, one of which uses libxml2. I am currently trying to switch to building them under msys2 and the latter one is hitting a problem. My updated Makefile includes this:
CFLAGS += -I/mingw32/include/libxml2
$(BINDIR)/%.o: %.c
#-$(MKDIR) $(BINDIR)
$(CC) $(CFLAGS) -c $*.c -o $#
But when I run make (mingw32-make) the compile fails as follows:
[csv-gen]: mingw32-make
gcc -std=c99 -Werror -Wall -D_XOPEN_SOURCE -O -I/mingw32/include/libxml2 -c xmlParse.c -o ../../bin/Win32/csv-gen/xmlParse.o
xmlParse.c:36:27: fatal error: libxml/parser.h: No such file or directory
#include "libxml/parser.h"
^
compilation terminated.
Makefile:66: recipe for target '../../bin/Win32/csv-gen/xmlParse.o' failed
mingw32-make: *** [../../bin/Win32/csv-gen/xmlParse.o] Error 1
Yet that libxml/parser.h file does exist under the /mingw32/include/libxml2 path given by the -I option:
[csv-gen]: ls /mingw32/include/libxml2/libxml/parser.h
/mingw32/include/libxml2/libxml/parser.h
And the strange thing is that if I run the exact same gcc command directly from the msys bash shell (copying and pasting it from the make output above) then it compiles fine with no errors.
And I have the exact same problem with the link phase where it doesn't find the libxml2.a library in the path given to gcc by -L/mingw32/lib, and again I run the gcc link command directly from the shell and it works fine.
So why would gcc's -I/mingw32/include/libxml2 and -L/mingw32/lib options not work when run via mingw32-make yet the exact same options work fine on calling gcc directly from the shell?
I did try making the paths explicit Windows paths (d:/msys64/mingw32/...) and also tried quoting them, to no avail.
It turned out that the answer was to use explicit Windows paths, which as I said above I had tried, but when I tried again it worked so I must have previously made some mistake doing that. I found this by using the output from pkg-config which gives:
[csv-gen]: pkg-config --cflags libxml-2.0
-ID:/msys64/mingw32/include/libxml2
So apparently the problem is that mingw32-make doesn't fully understand the msys filing system, or at least not /mingw32 and /mingw64 paths in it.

How to build a project without make (bare shell script)?

I heard that back in the old days (or maybe not so old), before the make utility was included in Unix, people used to write shell script to "make" and "install" their software.
Consider a project with: 2 source files main.c and util.c, a header util.h that uses the OpenGL library and needs to run on Ubuntu.
(Ubuntu and OpenGL are used just for the sake of being specific)
What would such a script actually need to do? Where can I find an example?
It's hard to imagine why anyone would want to revisit "the bad old days" before make, but it's actually not too difficult for a simple project. So given your particular example, a shell script to compile might look like this:
gcc -Wall -c main.c -o main.o -lglut -lglm
gcc -Wall -c util.c -o util.o -lglut -lglm
gcc -Wall main.o util.o -o main -lglut -lglm
What it does is to simply run through the entire "recipe" to build the project every time. The advantage is that, if the source code is all correct, it should result in an executable. The considerable disadvantages are that
if any step fails, so will subsequent steps that rely on the failed step
everything is rebuilt every time, wasting a lot of time
compile and link flags are all embedded into the script and hard to change
this approach has little hope of cross-platform compatibility
One can tinker with the basic shell script to improve on various aspects of this. Then, when that proves inadequate, one could write a program to do these things better. At that point, in essence, you will have re-invented make.

Can't output anything using cygwin 1.7 gcc under windows

I just installed cygwin 1.7, and wrote a simple Hello world in test.c
but when I complie, nothing happens, even no error messages
gcc-4 -o test.exe test.c
And there's nothing generated under my folder.
I have included C:\cygwin;C:\cygwin\bin in my PATH
Did I miss something?
EDIT:
for more information, I installed Qt4, tortoiseHg, and mingw before.
Now I had removed mingw. but still got Qt4 and tortoiseHg, is this a problem?
Try doing this from the Cygwin Terminal, not cmd.exe:
$ cd `cygpath -u "$USERPROFILE"`/Desktop/UT
$ gcc -o foo foo.c
$ ls -l foo
-rwxr-xr-x+ 1 yourlogin None 19618 May 10 05:15 foo*
If that works, there's some bogus remnant lying around.
You'll find that the Cygwin experience is generally better running under Bash, in a MinTTY terminal anyway. cmd.exe doesn't understand Cygwinisms, and is a DOS throwback besides.
Note that you don't need to say gcc-4 to get GCC 4.x. gcc is GCC 4.x on Cygwin, and has been for quite some time now.
Also note that you don't need to include .exe in the GCC -o flag, because Cygwin GCC knows to add that already.

Resources