Makefile in Windows - c

I have a folder named 'X'. In X I have two subfolders: src and include.
The src folder contains a C file main.c, and the include folder contains a file main.h.
I want to write a makefile to build these files(from folder X) in windows command prompt.
I have tried a lot but with no effect.
First of all I need the make file format in Linux for this, and once I have that i will do that in Windows.
Can anybody help me in doing this?

Try the following links, in there you'll find the format, and basically all that you need to build your Makefile:
http://mrbook.org/blog/tutorials/make/
http://www.opussoftware.com/tutorial/TutMakefile.htm
http://oucsace.cs.ohiou.edu/~bhumphre/makefile.html

Once you've made your Makefile, you can use MinGW's mingw32-make - A Windows native build of GNU make.

Open the folder (X) in file explorer.
Right click anywhere > new > text document.
Right click on the file > rename > remove .txt extension.
Open up the makefile with your favorite text editor.

You can use NMake, which is the windows alternative do Linux make. It is a command-line tool included with Visual Studio.
Although the syntax is very similar, you will find some differences. So the makefile that works on Linux may not work directly with NMake in windows.
In Linux, these lines for example:
gcc -c main.cpp -o main.o
gcc -o app main.o -lstdc++
In Windows, probably should be changed to:
cl.exe /c main.cpp /Fomain.o
link.exe /OUT:app.EXE main.o
So the makefile require edition to work with NMAKE in windows.
To use the makefile "as is", you should try a tool to mimic Linux commands in Windows, like Cygwin.

Related

Why gcc under Windows O.S. produces a .o instead of a .lib file when compiling static libraries?

I am using gcc 8.1.0 on Windows. To install it I set up Code::Blocks on my computer and updated the environment variable list by adding the path to the gcc.exe program within the installation folder of CodeBlocks. The file editor I used was the built-in editor in Visual Studio. The terminal to compile was the power shell from Visual Studio as well.
In the library development folder I have the files mul.c and mul.h. Their content is irrelevant.
To compile the library I use the command:
gcc -c mul.c
When I run it, it creates a file object mul.o and not mul.lib. I needed to use the option -o mul.lib to successfully create the desired extension file. After placing the header, the .lib file and the main.c in the same parent folder I am obvioudly able to build the executable by running.
gcc main.c -I./include -L/static -lmul -o my_program.exe
I have two questions:
Why does gcc produces a .o if I am in a Windows environment?
I followed a tutorial that compile the static library under Linux and it names it libmul.o, in this way the -lmul option is able to retrieve the library. But if I call my generated static library libul.lib it generates the error:
C:/Program Files/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-ingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lmul
collect2.exe: error: ld returned 1 exit status
Are these a normal behaviours by gcc, or is it side effect of making gcc available just by updating the Windows environmental variables list?
Thank you to the community in advance.
GCC comes from the *nix world where libraries have the .a extension. When using GCC+MinGW this remains the case.
Shared libraries in MinGW are .dll files but their libraries for linking are .dll.a files.
The advantage of .a files is that a lot of sources build out of the box on Windows with MinGW, especially when using MSYS2 shell.
If you use -l it will look for .a (or .dll.a for shared build) file adding the lib prefix and the extension automatically.
So -lmul will look for libmul.a (static, e.g. when --static linker flag is given) or libmul.dll.a (shared).
By the way, you are using quite an old GCC 8.1.0.
As I write this current version is 12.2.0. Check https://winlibs.com/ for a standalone download (instructions on how to configure in Code::Blocks are on the site) or use MSYS2's package manager pacman.

Link a makefile for using SDL librarie

I'm learning using the SDL librarie, with a french site.
This is the tuto :
"There may be some of you who have gotten into the habit of compiling by hand under Linux using a Makefile (file controlling compilation).
If this is your case, I invite you to download a Makefile which you can use to compile SDL projects.
The only thing a little special is the addition of the SDL library for the linker (LDFLAGS). You will have to download the Linux version of the SDL and install it in your compiler folder"
I've found my gcc folder in /usr/bin/gcc , but when I want to move the makefile to this folder, the error is :
mv: cannot move 'makefile_sdl' to '../usr/bin/makefile_sdl': Permission denied
There it is my make file, maybe something's wrong or missing (i'm a beginner ahah)
CPP=gcc #compiler command
CFLAGS=-O3 #Option d'optimisation du programme
LDFLAGS=-lSDL -lSDL_mixer #Linker
EXEC=nomProgramme #Nom du programme à modifier
all: ${EXEC}
${EXEC}: ${EXEC}.o
${CPP} $(CFLAGS) -o ${EXEC} ${EXEC}.o ${LDFLAGS}
${EXEC}.o: ${EXEC}.c
${CPP} $(CFLAGS) -o ${EXEC}.o -c ${EXEC}.c
You don't need to (and shouldn't) move the makefile to /usr/bin.
You can put it anywhere, but things will be easier if it's in the same directory as your source code (nomProgramme.c).
You need to name your Makefile Makefile. Case sensitive, make sure to check for spelling errors, etc. You also need to be in the same directory as Makefile when you run make.
You should also just run make without any specified targets. It will automatically build the all target, which will build your program into an executable called nomProgramme.
You'll also need to install libsdl-mixer1.2-dev (sudo apt-get install libsdl-mixer1.2-dev and change EXEC=main back to EXEC=nomProgramme.
Here's a link to the GNU make documentation. It gives a quick overview and then a very detailed explanation of everything you can do with GNU make: https://www.gnu.org/software/make/manual/make.html
to answer the OPs question:
the directory /usr/bin is not writable via user (like yourself).
However, executing a command line that is prefixed with sudo (giving you 'root' privileges) will enable you to write on that directory.
However, /usr/bin is intended for executable files and Makefile_sdl is NOT an executable file
Suggest saving that file 'somewhere' Then when you want to use that makefile, to copy it to the directory where your current project is located (I.E. the directory in which you want to run make
However, Makefile_sdl is not one of the standard file names recognized by make so must run make via:
make -f Makefile_sdl

MinGW_w64 missing dll's

I am trying to run a C compiler with Cython in a 64-bit Windows 7 platform.
After having various problems with the C compilers from Visual studio and MinGW32, I tried to install MinGW_w64. I did the following steps :
-I downloaded and extracted winbuilds from http://mingw-w64.org/doku.php/download/win-builds, and I selected the Base GCC package with C support.
-I added C:\PATH_TO_WINBUILDS and C:\PATH_TO_WINBUILDS\bin in the PATH Windows environment variable.
-I wrote helloworld.c, a very simple C program :
#include<stdio.h>
main()
{
printf("Hello World");
}
-Then in cmd I wrote:
gcc helloworld.c -o helloworld.exe
And a dialog box pops out with the following error message :
Impossible to start program because libiconv.dll is missing on your computer. Reinstall program to correct problem.
The dll file does not appear in the lib folder of MinGW_w64. I tried to copy libiconv.a and libiconv.dll.a from the MinGW32 lib folder to the lib folder of MinGW_64, but still no luck.
Any ideas?
You need the DLL file, not the .a link library files. libiconv.dll should exist somewhere in the bin directory. If it is not there, you’ll need to get it from the internet or compile it yourself. Alternatively you could just link statically to the iconv library.
Once you find the DLL, make sure it is in one of the following locations:
* the %PATH%
* the appropriate C:\Windows\system32 or C:\Windows\SysWOW64 directory
* (preferably) next to your executable
I was able to fix the problem by downloading another version on the package on SourceForge.
Thanks!

How to execute and compile a program in Xcode via command line in c and where's the executable?

I'm trying to execute via command line a code written in C. I tried gcc -o file file.c, but it did not work. I need to learn how to compile and execute a code using gcc and llvm without graphical interface. Furthermore when I compile the program I cannot find the executable file in Finder (there's no Developer folder in Library).
Thanks in advance.
You can use xcrun tool:
#/usr/bin/xcrun cc -o file file.c
Note: if you have several Xcode versions you can chose with xcode-select and your command above will use compiler and the rest of the tools from the selected SDK.
If file.c is in the Desktop directory: Did you change to that directory beforehand?
Usually Terminal.app starts in the home directory, e.g.: /Users/yourname
To get to the Desktop directory:
cd ~/Desktop
Then check if the source file is there:
ls -l file.c
Then try again to compile:
gcc -o file file.c
Check for any error messages. If no output is given everything is fine and there should be an executable which can be (surprise!) executed:
ls -l file
./file

How to compile makefile using MinGW?

I'm new to this. Just wanted to ask how to compile a makefile. I am using MinGW compiler in C language. Do I have to save all my files in MinGW\bin? because right now my files are in a different directory.
Appreciate the help.
Excerpt from http://www.mingw.org/wiki/FAQ:
What's the difference between make and mingw32-make?
The "native" (i.e.: MSVCRT dependent) port of make is lacking in some functionality and has modified functionality due to the lack of POSIX on Win32. There also exists a version of make in the MSYS distribution that is dependent on the MSYS runtime. This port operates more as make was intended to operate and gives less headaches during execution. Based on this, the MinGW developers/maintainers/packagers decided it would be best to rename the native version so that both the "native" version and the MSYS version could be present at the same time without file name collision.
So,look into C:\MinGW\bin directory and first make sure what make executable, have you installed.(make.exe or mingw32-make.exe)
Before using MinGW, you should add C:\MinGW\bin; to the PATH environment variable using the instructions mentioned at http://www.mingw.org/wiki/Getting_Started/
Then cd to your directory, where you have the makefile and Try using mingw32-make.exe makefile.in or simply make.exe makefile.in(depending on executables in C:\MinGW\bin).
If you want a GUI based solution, install DevCPP IDE and then re-make.
You have to actively choose to install MSYS to get the make.exe. So you should always have at least (the native) mingw32-make.exe if MinGW was installed properly. And if you installed MSYS you will have make.exe (in the MSYS subfolder probably).
Note that many projects require first creating a makefile (e.g. using a configure script or automake .am file) and it is this step that requires MSYS or cygwin. Makes you wonder why they bothered to distribute the native make at all.
Once you have the makefile, it is unclear if the native executable requires a different path separator than the MSYS make (forward slashes vs backward slashes). Any autogenerated makefile is likely to have unix-style paths, assuming the native make can handle those, the compiled output should be the same.
I have MinGW and also mingw32-make.exe in my bin in the C:\MinGW\bin . same other I add bin path to my windows path. After that I change it's name to make.exe . Now I can Just write command "make" in my Makefile direction and execute my Makefile same as Linux.
First check if mingw32-make is installed on your system.
Use mingw32-make.exe command in windows terminal or cmd to check,
else install the package mingw32-make-bin.
then go to bin directory default ( C:\MinGW\bin) create new file make.bat
#echo off
"%~dp0mingw32-make.exe" %*
add the above content and save it
set the env variable in powershell
$Env:CC="gcc"
then compile the file
make hello
where hello.c is the name of source code
Please learn about automake and autoconf.
Makefile.am is processed by automake to generate a Makefile that is processed by make in order to build your sources.
http://www.gnu.org/software/automake/
I found a very good example here: https://bigcode.wordpress.com/2016/12/20/compiling-a-very-basic-mingw-windows-hello-world-executable-in-c-with-a-makefile/
It is a simple Hello.c (you can use c++ with g++ instead of gcc) using the MinGW on windows.
The Makefile looking like:
EXECUTABLE = src/Main.cpp
CC = "C:\MinGW\bin\g++.exe"
LDFLAGS = -lgdi32
src = $(wildcard *.cpp)
obj = $(src:.cpp=.o)
all: myprog
myprog: $(obj)
$(CC) -o $(EXECUTABLE) $^ $(LDFLAGS)
.PHONY: clean
clean:
del $(obj) $(EXECUTABLE)

Resources