views:

2965

answers:

6

Hi, 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. Anyway 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

So anyway, 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'd 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?

EDIT: shameless bump

A: 

MSVC++6 is getting on a bit but you didn't mention how you got on with MSVC++9 and nmake (visual c++ express 2008).

Tim Matthews
err... maybe i didn't explain well enough..
krebstar
+3  A: 

This is not an answer to your question but I suggest to use cmake . It's a multi-platform multi-build environement project file generator. The syntax of CMake files is quite simple (certainly simpler than Makefile) and it will generate Makefile or Visual Studio projects on request.

Problem with hand-coded Makefile is that very soon, your dependency tree grows so big that maintaining all the rules of what needs to build what becomes a very tedious operation. Hence, there are tons of Makefile generators (autotools, CMake) or Makefile alternatives (Scons, waf, bjam, ....)

Bluebird75
+2  A: 

Use -L to specify a directory where gcc should search for libraries. Use -l (lower-case ell) to specify a library to be linked from the -L paths (and some defaults).

To escape paths with spaces, use backslashes, single or double quotes as your shell requires. See the GNU make documentation, the GNU bash documentation. See this article for info about spaces in other parts of GNU makefiles.

David Schmitt
So I have to specify both options? Can you give me an example? Would just specifying -L suffice?
krebstar
+1 btw :) Like the part about -l..
krebstar
Yes, you'll need to specify both the library and the path where GCC can find the lib. Both parameters can be given multiple times with different values. In the end all symbols (e.g from the Common RunTime) have to be satisfied in some library or object file.
David Schmitt
+6  A: 

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
Bluebird75
Thanks, will check this out tomorrow..
krebstar
I did what you said, I'm still getting some errors though. It's still choking on "undefined reference to WSAStartup@8".. Any ideas?
krebstar
It seems to also be trying to compile crt2.c and crt1.c in the MinGW directories.. I'm getting stuff like C:/MinGw/bin../lib/gcc/..blahblah/crt2.o: crt1.c:(.text+0x3): undefined reference to '_imp__atexit'
krebstar
+1  A: 

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

n0rd
I'm using mingw, how do I go about this? So this means I can't simply use the lib from MSVC6?
krebstar
MinGW comes with it's own Win32 API headers and libraries as well (check the "include" and "lib" folders in MinGW install folder, there should be files like windows.h and libws2_32.a), you should use them. And I don't know if there is a way to use MSVC libs in MinGW.
n0rd
I just checked the MinGW 5.1.3 installer: there is a checkbox that allows installing of Win32 API files.
n0rd
I checked and the libfiles are installed, it's called libws2_32.a. I changed my LINKFLAGS variable to say C:\MinGW\lib and the -l option to -lws2_32, but it still complains of an undefined reference to WSA_Startup@8.. :S
krebstar
Looks like -l switches should follow list of .o files, otherwise it will not link (try "$(CC) -o main.exe main.o Accel.o $(LINKFLAGS)" - with $(LINKFLAGS) at the end).
n0rd
A: 
LIBS = -L"..\Microsoft SDKs\Windows\v6.0A\Lib" -lWS2_32

$(CC) ... -o ... ... $(LIBS)

This syntax worked for me!

marrog