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)
Related
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
I am a newbie student learning C and wish to use the gLib library functions for a project: http://www.linuxfromscratch.org/blfs/view/svn/general/glib2.html
(I am using Ubuntu)
I have a couple questions about how libraries work in C and what happens when you install one or want to use one:
When I install this (run ./configure && make && make install inside the folder), what exactly is it doing? From what I learned there are shared libraries in C and static libraries in C. Is it true that it is installing library and include files to /usr/lib/ or somewhere?
When using gcc with external libraries, you have to specify -L and -I flags to specify where to look for library and header files. When I install glib, will I need to specify these flags?
If I want to package my executable for another machine, what would happen if the other machine doesn't have glib? I think if I had static libraries I would be able to include it in the binary, but how would it work for glib?
I am familiar with developing with GTK+ and GLIB. As i'm aware library files reside in /usr/lib and include files are found in /usr/include. Some libraries might be in places such as /usr/local/lib. I will attempt to answer your questions as best as I could.
When installing a library through the source package yes it installs files to the various folders /usr/share /usr/lib /usr/include and etc. It's highly recommended you use your distribution's package manager to install library packages and development headers. Installing from source is always bound to fail as necessary dependencies might be required.
This is where tools such as autogen and makefiles come handy. You don't necessarily need to concern yourself with specifying all that. tools such as pkg-config handle all that work. Most libraries will install a package configuration file into /usr/lib/pkgconfig & /usr/share/pkgconfig directories. This helps anyone developing an application easily link their code to the libraries.
Using package config to get the config:
$ pkg-config --cflags --libs glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -lglib-2.0
Linking using GCC & package config:
$gcc example.c `pkg-config --cflags --libs gtk+2.0 glib-2.0` -o example
The above command would link my program with gtk & glib.
Using Makefile to not ever have to enter those long lines again:
Makefile:
OBJS = main.o callbacks.o
CFLAGS = `pkg-config --cflags --libs gtk+-2.0`
program: $(OBJS)
gcc -o program $(OBJS)
main.o: main.c
gcc -c main.c $(FLAGS)
callbacks.o: callbacks.c callbacks.h
gcc -c callbacks.c $(FLAGS)
.PHONY : clean
clean:
rm *.o
rm program
.PHONY : install
install:
cp program /usr/bin
.PHONY : uninstall
uninstall:
rm /usr/bin/program
The above makefile is for a simple GTK+2.0 application as you can tell by what package config is including in CFLAGS to make the program executable all you have to enter in your source directory would be make. pkg-config will only work if you have installed the development packages for the library you are trying to work with. For ubuntu to install GTK+-3.0 and GLIB development files you would enter:
$ apt-get install libgtk-3-dev
I think this is a good concern for portability. No single static library is going to be cross platform. It would have to be compiled for those platforms manually. I reckon to get rid of all the headache you would use Anjuta IDE developed by the GNOME software foundation. It makes developing GLIB & GTK+ apps a breeze supporting both C & C++. It will create the Makefile, configure and other files to make developing code on cross platforms easy and make deployment easy. I could link you some resources, but my reputation on stack overflow is less then 10. So I will just mention the name of some resources below.
Further Reading
Makefile Tutorial
Anjuta IDE (C/C++)): ://anjuta.org/
GTK+-3.0 Hello World with Compiling and linking using pkg-config:
When I install this (run ./configure && make && make install inside
the folder), what exactly is it doing? From what I learned there are
shared libraries in C and static libraries in C. Is it true that it is
installing library and include files to /usr/lib/ or somewhere?
Well it is running first ./configure and then if that succeeds it runs make and if that succeeds it runs make install. configure is a script that takes care of a lot of compatibility issues between systems. They are usually shell scripts as this is the common denominator across systems so the configure script will work across various systems. One of the thing configure does is create a Makefile. The second command make will use the newly created Makefile to build the library or executable. Since you did not specify a target (like you will in the make install) make will build the default target, which is typically the all target. This is just by convention. Makefiles are basically a list of things to build (targets) along with what they depend on (dependencies) and how to build to target (rules). Finally, make install will actually install the necessary components. For libraries this is the library and necessary header files for executables it is just the program. man pages might also be installed. Where you install the libraries depends on where you specify to install them. Typically configure will take the --prefix argument that lets you control where they are installed. If you do not use --prefix you will most likely install in the default location for your system.
When using gcc with external libraries, you have to specify -L and -I
flags to specify where to look for library and header files. When I
install glib, will I need to specify these flags?
Your question is a little unclear, so let me first make sure I understand. Are you asking if after you install glib will you need to use -L and -I to tell gcc where to look for them? If so it depends on where you install them. Typically when you make and install a library you will install the library and header files in the default location or not. If you did then assuming your gcc is configured correctly then no you will not. If you did not then you will most likely have to use -L and -I
If I want to package my executable for another machine, what would
happen if the other machine doesn't have glib? I think if I had static
libraries I would be able to include it in the binary, but how would
it work for glib?
If it doesn't have glib and you used the shared libraries your application will not work. You will need to either have the same version glib libraries on the other machine or build the libraries statically. How to build them statistically depends on the library. This SO question might help.
Regarding configure, make and make install. configure is a shell script that is used to discover (and configure) your development environment. make and make install are convenient way of building your software. Where make would normally involve compiling and linking, where as make install would normally involve copying executables and libraries to standard path and setting up things (also include files if any usually in /usr/include), so that you don't have to explicitly give path before running the executable. What make does can be done by hand, but it's very cumbersome.
For glibc - yes you have to specify those flags. Normally all libraries will come in two flavors on most of the platforms. The binary form are used for dynamic linking when programs are actually loaded. Also - most distributions will have -dev or -devel versions of those libraries. Those are required for building software that makes use of those libraries (configure above can help find out whether devel libraries are installed). Typically when you see a library installed but not it's devel - you are likely to see configure errors. In short you require devel versions if you want to link with those libraries. This step is not needed if you are building libraries also from source using make and make install.
If you want to package your executable for another machine and you are not sure whether another glib is there or you want to be sure that the glib to be installed should be one specific version that you want, you should statically link while building (compiling/linking) the library. this gcc man page has got several details about link options. I believe there should be a way to statically link glib(or glib2). Though normally that may not be required if you have enough applications that are using it already.
Resently I'm installed Opencv in my machine. Its working in python well(I just checked it by some eg programs). But due to the lack of tutorials in python I decided to move to c. I just run an Hello world program from http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/
while compiling I got the following error
hello-world.c:4:16: fatal error: cv.h: No such file or directory
compilation terminated.
I'm new in opencv
Qn : Could you please report what may be the problem - and how I run my helloworld program in c?
Your compiler cannot find your cv.h include file. If you installed from your package manager, it is probably in /usr/include/opencv/. You need to add that your include search path. If you are compiling from the command line use -I to specify additional include directories. It will be something like -
gcc -I /usr/include/opencv/ -o helloworld helloworld.c
If you are using Eclipse,
Right click on the project and select properties.
Select C/C++ General -> Path and Symbols.
Select Includes tab.
In Languages list, select 'GNU C' or 'GNU C++' depending on which you are using.
Press 'Add...' button and add /usr/include/opencv/
Save and rebuild.
You need to show compiler path to cv.h file. The quick way to find it is to do (on Ubuntu):
find /usr -name "cv.h"
/usr/local/include/opencv/cv.h
Just add this to the compiler:
gcc -I/usr/local/include/opencv -o helloworld helloworld.c
Since you asking this question your compiler might also have problems linking your program to opencv libraries. Just do the same thing only for library files:
find /usr -iname "libopencv*"
/usr/local/lib/libopencv_flann.so
...
add this folder the same way and specify libraries you want to use:
gcc helloworld.c -I/usr/local/include/opencv -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -o helloworld
that should probably compile. There is a also a short cut you can take and instead of all that steps just use the following command
gcc helloworld.c `pkg-config --cflags --libs opencv` -o helloworld
that should take care of all the work of locating required files for you and let you focus on the fun coding part.
maybe you just installed the opencv package.
But, as you want to use opencv in your C program, you may also install the package named just like opencv-devel. If you haven't, install it and than use it as #iagreen said.
Best wishes to you.
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.
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.