带有 g++ 的 Windows 上的 Makefile,链接库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/577006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:01:53  来源:igfitidea点击:

Makefiles on windows with g++, linking a library

c++windowsmakefileg++

提问by krebstar

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

我已经厌倦了 MSVC++6 以及每个人总是告诉我它是一个蹩脚的编译器等等。

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

所以现在我决定尝试使用 vim 加上 g++ 和 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 includein one of my .hfiles.

这在尝试时会出错make,因为我需要链接到一个名为 的 Windows 库Ws2_32.lib,这是Winsock2.hinclude在我的一个.h文件中需要的。

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

那么我该怎么做呢?我试过这个-l选项,但我不能让它工作。它如何处理有空格的路径?

回答by Philippe F

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 :

第二步,使用 -L 传递该目录:

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

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

第三步,用-l(小写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 

回答by Philippe F

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.

这不是您问题的答案,但我建议使用cmake。它是一个多平台多构建环境项目文件生成器。CMake 文件的语法非常简单(当然比 Makefile 更简单),它会根据要求生成 Makefile 或 Visual Studio 项目。

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, ....)

手工编码的 Makefile 的问题是,很快,您的依赖树就会变得如此之大,以至于维护构建所需的所有规则变得非常乏味。因此,有大量的 Makefile 生成器(autotools、CMake)或 Makefile 替代品(Scons、waf、bjam....)

回答by David Schmitt

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

使用-L指定的目录中的gcc应搜索库。使用-l(小写 ell)指定要从-L路径(和一些默认值)链接的库。

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.

要使用空格转义路径,请根据 shell 需要使用反斜杠、单引号或双引号。请参阅GNU make 文档GNU bash 文档。有关GNU makefile 其他部分中的空格的信息,请参阅这篇文章。

回答by n0rd

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).

您应该与编译器提供的库链接。在 cygwin 的情况下,你应该安装 win32 api 头文件和库文件,然后与 libws2_32 链接(编译器选项看起来像 -lws2_32)。

回答by marrog

LIBS = -L"..\Microsoft SDKs\Windows\v6.0A\Lib" -lWS2_32

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

This syntax worked for me!

这种语法对我有用!

回答by Tim Matthews

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).

MSVC++6 有点进展,但你没有提到你是如何使用 MSVC++9 和 nmake(visual c++ express 2008)的。