Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefiles on windows with g++, linking a library

I've gotten fed up with MSVC++6 and how everyone is always telling me that it's a crappy compiler and such.

So now I've decided to try to use vim plus g++ and makefiles. Here's my problem; I have the following makefile:

# This is supposed to be a comment..
CC = g++
# The line above sets the compiler in use..
# The next line sets the compilation flags
CFLAGS=-c -Wall

all: main.exe

main.exe: main.o Accel.o
    $(CC) -o main.exe main.o Accel.o 

main.o: main.cpp Accel.h
    $(CC) $(CFLAGS) main.cpp

Accel.o: Accel.cpp Accel.h
    $(CC) $(CFLAGS) Accel.cpp

clean:
    del main.exe *.o

This gives an error when trying to make, because I need to link to a windows library called Ws2_32.lib, which is needed by Winsock2.h, which I include in one of my .h files.

So how do I do this? I've tried the -l option, but I can't make it work. How does it work with a path that has spaces?

like image 204
krebstar Avatar asked Feb 23 '09 09:02

krebstar


People also ask

Can Windows run makefiles?

Before running a Makefile in Windows, it is required to install the make command first by using the “Mingw-get install mingw32-make” command on the Command Prompt. Then, create a Makefile, remove the “. txt” extension, and use the “make” command to run the specified Makefile in Windows.

Are makefiles still used?

make was first developed in 1976, and yet it's still widely used, predominantly in application development where you need to compile binaries. While makefiles are quite uncommon in Web development in general, at SSENSE we have found an unconventional use-case for it.

Where does make look for libraries?

The make command itself does not search for libraries or header files - instead it looks for a Makefile in the current directory (unless an alternative file is specified on the command line using the -f option) and executes the instructions inside.


1 Answers

First step: locate the library you are looking for. For me, it's in :

C:\Program Files\Microsoft Visual Studio\VC98\Lib

Second step, pass that directory with -L :

LINKFLAGS=-L"C:\Program Files\Microsoft Visual Studio\VC98\Lib"

Third step, pass the name of the library with -l (lowercase L):

LINKFLAGS=-L"C:\Program Files\Microsoft Visual Studio\VC98\Lib" -lWs2_32

Then use it:

main.exe: main.o Accel.o
   $(CC) $(LINKFLAGS) -o main.exe main.o Accel.o 
like image 90
Philippe F Avatar answered Oct 19 '22 22:10

Philippe F