Makefile in windows with g ++, library linking

I'm fed up with MSVC ++ 6 and as everyone always tells me it's a crappy compiler, etc.

So, now I decided to try using vim plus g ++ and make files. Here is my problem; I have the following make file:

# 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 leads to an error when trying to make , because I need to link to the Windows library named 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 tried the -l option, but I can't get it to work. How does this work with empty space?

+4
source share
6 answers

First step: find the library you need. For me, this is in:

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

Second step, pass this directory with -L:

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

Third step, pass the library name 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 
+8
source

This is not an answer to your question, but I suggest using cmake . It is a multi-platform multi-project file generator for the environement project. The syntax of the CMake files is quite simple (of course, simpler than the Makefile), and it will generate Makefile or Visual Studio projects upon request.

The problem with the Makefile encoding manually is that very soon your dependency tree gets so large that it supports all the rules of what needs to be built, which becomes a very tedious operation. Consequently, there are many Makefile generators (autotools, CMake) or Makefile generators (Scons, waf, bjam, ....)

+3
source

Use -L to specify the directory where gcc should look for libraries . Use -L (lowercase ell) to specify the library to be associated with the -L paths (and some default values).

To avoid paths with spaces, use backslashes, single or double quotes as needed. See GNU make documentation , GNU bash documentation . See this article for information on spaces in other parts of GNU make files .

+2
source

You should link the library to the compiler. In the case of cygwin, you must set the headers and win32 api library files, then link to libws2_32 (the compiler option will look like -lws2_32).

+2
source

MSVC ++ 6 spoils a little, but you did not specify how you are with MSVC ++ 9 and nmake (visual C ++ express 2008).

0
source
 LIBS = -L"..\Microsoft SDKs\Windows\v6.0A\Lib" -lWS2_32 $(CC) ... -o ... ... $(LIBS) 

This syntax worked for me!

0
source

All Articles