problems running MAKE utility from DOS command prompt - compilation

I have:
fileMainProgram.cpp
fileClassImplementation.cpp
fileClass.h
makefile
in a directory.
Ran cmd and typed
g++ -make -f makefile
got this message after tinkering with it for a while (change file name/extension, tried without -f, used gcc instead of g++, etc)
C:\miscprograms\Dropbox\box\Dropbox\c++\etextbook\e12\progec12\pe1c12romanNumeral>g++ -make -f makefile
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe:makefile.tx
t: file format not recognized; treating as linker script
c:/mingw/bin/../lib/gcc/mingw32/4.6.2/../../../../mingw32/bin/ld.exe:makefile.tx
t:1: syntax error
collect2: ld returned 1 exit status
This is the makefile, (tried to delete the lines inbetween targets and actions, tried using automatic variables, etc,
I tried to compile the program as
g++ -c fileClassImplementation.cpp
then
g++ fileClassImplementation.o fileMain.cpp
to get an executable and it works fine.
all: a
a: romanNumeralMain.o romanNumeralImp.o
[1 tab]g++ romanNumeralMain.o romanNumeralImp.o -o a
romanNumeralMain.o: romanNumeralMain.cpp
g++ -c romanNumeralMain.cpp
romanNumeralImp.o: romanNumeralImp.cpp
g++ -c romanNumeralImp.cpp
clean:
rm romanNumeralImp.o romanNumeralMain.o a
Ran the whole thing on C4droid for my samsung phone and worked fine as is.
Doesn not work on my Windows7 laptop.

What you appear to have is a GNU Makefile, suitable for running GNU Make. Windows doesn't come with GNU Make, so you'll need to download it yourself.
It appears you've already downloaded MingW, try running the mingw32-make command to execute your makefile.

Related

Include path problem when compiling wxWidgets app

I'm now compiling wxWidgets' hello world sample: https://docs.wxwidgets.org/trunk/overview_helloworld.html
using MingGW-w64 and it's mingw32-make on windows, and I'm using the pre-compiled Windows binaries it provides.
I have already compile successfully using
g++ -o hello -I/d/local/wxWidgets/lib/gcc810_x64_dll/mswu -I/d/local/wxWidgets/include -L/d/local/wxWidgets/lib/gcc810_x64_dll hello.cpp -lwxbase31u -lwxmsw31u_core
(I run this git-bash), and it can be execute normally too.
But when I copy the same line into makefile like this:
all: hello
hello: hello.cpp
g++ -o hello -I/d/local/wxWidgets/lib/gcc810_x64_dll/mswu -I/d/local/wxWidgets/include -L/d/local/wxWidgets/lib/gcc810_x64_dll hello.cpp -lwxbase31u -lwxmsw31u_core
and run with mingw32-make.exe in terminal, this error happened:
hello.cpp:3:10: fatal error: wx/wxprec.h: No such file or directory
#include <wx/wxprec.h>
^~~~~~~~~~~~~
compilation terminated.
mingw32-make: *** [makefile:3: hello] Error 1
I'm not sure if it's the including path problem, because It actually compile successfully when I run the same line in terminal directlty.
Are you also using the "git bash" terminal when you try with your Makefile ?
Regards
Xav'
After trying executing in cmd and git-bash a few times. I found it's probably the path style problem. I tried 2 cases of path specification below:
First:
g++ -o hello -I/d/local/wxWidgets/lib/gcc810_x64_dll/mswu -I/d/local/wxWidgets/include -L/d/local/wxWidgets/lib/gcc810_x64_dll hello.cpp -lwxbase31u -lwxmsw31u_core
Second:
g++ -o hello -ID:\local\wxWidgets\lib\gcc810_x64_dll\mswu -ID:\local\wxWidgets\include -LD:\local\wxWidgets\lib\gcc810_x64_dll hello.cpp -lwxbase31u -lwxmsw31u_core
Git-bash(execute in terminal directly): only first case successfully compiled.
Git-bash(using mingw32-make): only second case successfully compiled.
cmd(execute in terminal directly): only second case successfully compiled.
cmd(using mingw32-make): only second case successfully compiled.
Conclusion: I think mingw32-make using "traditional" Windows path style (backslash), even under git-bash.
Update:
I found that mingw32-make also support forword slash, so I can rewrite as:
g++ -o hello -ID:/local/wxWidgets/lib/gcc810_x64_dll/mswu ...
So I think it's better to use relative path in makefile, to prevent that (D:) in path, and also more portable(unix/linux use backslash too).

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.

Can't Run GCC on Cygwin Windows

I'm trying to compile some C files on my Windows. (already tried to change permissions with chmod)
Here is the error i'm getting:
$ gcc -c hello.c hello.o
bash: /usr/bin/gcc: cannot execute binary file
I have checked, and this is my PATH on the environment variables:
C:\cygwin\bin;C:\cygwin\usr\local\bin
When I type only gcc i'm getting the same error, instead of getting a Missing arguments error.
the which command responds as:
$ which gcc
/usr/bin/gcc
So, i'm stuck here.
Any help?
Thanks!

Clang++ -c outputs files to the wrong directory

I have this makefile target that contains these steps:
...
cd $(GRAPHICS_BUILD_DIR)
clang++ -c -I$(SFML_HEADERS) $(GRAPHICS_DIR)/*.cpp
cd $(BASE_DIR)
...
For some reason, Clang outputs the build files into BASE_DIR, not GRAPHICS_BUILD_DIR. Everything compiles fine, and when I execute one line at a time manually, it works fine, and the *.o files are outputted in the correct directory.
Why doesn't make put these files in the correct directory, and how can I force it to?
I'm using clang3.1 and the current version of GNUMake on ubuntu linux kernel 3.2.0-26.
The trouble is that in a Make rule, each command executes in its own subshell; nothing is remembered from one line to the next. So your first command starts in $(BASE_DIR), moves into $(GRAPHICS_BUILD_DIR), and dies. Your second command starts in $(BASE_DIR) and runs Clang there.
Try this:
...
cd $(GRAPHICS_BUILD_DIR) ; clang++ -c -I$(SFML_HEADERS) $(GRAPHICS_DIR)/*.cpp
...

Resources