C++ MingW静态库链接-sfml2.1

C++ MingW静态库链接-sfml2.1,c++,mingw,undefined-reference,C++,Mingw,Undefined Reference,我正试图让SFML2.1与MingW一起工作,但这会导致问题 我在MingW编译器中的编译行是: g++ -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics -lsfml-window -lsfml-system 我正在尝试链接.a文件(这是否意味着我应该将soemthing添加到eh编译行?) 代码如下: #include <SFML/Graphics.hpp> int main() { // cre

我正试图让SFML2.1与MingW一起工作,但这会导致问题

我在MingW编译器中的编译行是:

g++ -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics -lsfml-window -lsfml-system
我正在尝试链接.a文件(这是否意味着我应该将soemthing添加到eh编译行?)

代码如下:

#include <SFML/Graphics.hpp>

int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

// run the program as long as the window is open
while (window.isOpen())
{
    // check all the window's events that were triggered since the last iteration of      the loop
    sf::Event event;
    while (window.pollEvent(event))
    {
        // "close requested" event: we close the window
        if (event.type == sf::Event::Closed)
            window.close();
    }

    // clear the window with black color
    window.clear(sf::Color::White);

    // draw everything here...
    // window.draw(...);

    // end the current frame
    window.display();
}

return 0;
}
我做错了什么?错误表示未定义引用,这意味着存在无法找到的内容(库?)


SFML 2.1软件包附带了调试库和发布库(libsfml--s-d),这与此有关吗?

您首先要链接动态库,所以通过在库中添加-s后缀来更改静态库

您还需要添加SFML_静态预处理器定义

g++ -DSFML_STATIC -ID:\SFML-2.1\include -LD:\SFML-2.1\lib main.cpp -lsfml-graphics-s -lsfml-window-s -lsfml-system-s

至少那时您将链接到发布静态库。

整个需求在:

  • 添加
    -DSFML_STATIC
    预处理器标志

  • 使用后缀为
    -s
    -lsfml-graphics-s
    的库文件,而不是
    -lsfml graphics

  • 添加所需的所有静态库:
    -lopengl32-lwinmm-lgdi32
    是使用图形、窗口和系统模块时的最低要求

  • 简单的Makefile如下所示:

    EXE := static.exe
    SRC := $(wildcard *.cpp)
    OBJ := $(SRC:.cpp=.o)
    DEP := $(OBJ:.o=.d)
    
    CPPFLAGS := -Ipath/to/SFML/include -MMD -MP -DSFML_STATIC
    CXXFLAGS := -std=c++11 -Wall -W -pedantic
    LDFLAGS  := -Lpath/to/SFML/lib
    LDLIBS   := -lsfml-graphics-s -lsfml-window-s -lsfml-system-s
    LDLIBS   += -lopengl32 -lwinmm -lgdi32
    
    .PHONY: all clean
    
    all: $(EXE)
    
    $(EXE): $(OBJ)
        $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
    
    clean:
        $(RM) $(EXE) $(OBJ) $(DEP)
    
    ifeq "$(MAKECMDGOALS)" ""
    -include $(DEP)
    endif
    

    要卸下控制台,请将
    -mwindows
    链接器标志添加到
    LDFLAGS

    中;你的祈祷在我看来是正确的。尝试将静态库与
    -DSFML_static
    一起使用。我曾尝试下载不同版本的SFML 2.1,现在它已编译(耶!:D),但程序无法运行,我收到一个错误:0xc000007b。库是32位的,但我的操作系统是64位的。这有关系吗?不应该;x86-64与x86兼容(或者您的许多程序无法工作)。不过,您可能也应该确保您的代码也编译为32位。该错误意味着它找不到某些共享库(
    .dll
    ,在windows下)。请参阅调试/发布可能与此有关,以及动态与静态运行时。在windows上,调试运行时和发布运行时互不兼容,编译时必须进行选择。您还必须选择是在编译时链接静态库还是共享库(需要特殊定义)。不幸的是,我不记得MinGW上的特定选项组合。
    EXE := static.exe
    SRC := $(wildcard *.cpp)
    OBJ := $(SRC:.cpp=.o)
    DEP := $(OBJ:.o=.d)
    
    CPPFLAGS := -Ipath/to/SFML/include -MMD -MP -DSFML_STATIC
    CXXFLAGS := -std=c++11 -Wall -W -pedantic
    LDFLAGS  := -Lpath/to/SFML/lib
    LDLIBS   := -lsfml-graphics-s -lsfml-window-s -lsfml-system-s
    LDLIBS   += -lopengl32 -lwinmm -lgdi32
    
    .PHONY: all clean
    
    all: $(EXE)
    
    $(EXE): $(OBJ)
        $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
    
    clean:
        $(RM) $(EXE) $(OBJ) $(DEP)
    
    ifeq "$(MAKECMDGOALS)" ""
    -include $(DEP)
    endif