简体   繁体   English

文件之间的 C++ 依赖目录

[英]C++ Dependencies directories between files

My compiler has a problem to search files in the directories.我的编译器在搜索目录中的文件时遇到问题。 I'm on windows 10.我在 Windows 10 上。

First of all, my files are organized in this way:首先,我的文件是这样组织的:

  • C:/mingw64/bin/g++ (my compiler is here) C:/mingw64/bin/g++(我的编译器在这里)
  • C:/Users/Christophe/Documents/main.cpp (my file) C:/Users/Christophe/Documents/main.cpp(我的文件)
  • C:/Users/Christophe/Documents/SFML/include (my files.hpp) C:/Users/Christophe/Documents/SFML/include (我的文件.hpp)
  • C:/Users/Christophe/Documents/SFML/lib (???) C:/Users/Christophe/Documents/SFML/lib (???)

I have 9 files .hpp which are:我有 9 个文件 .hpp,它们是:

  • Audio.hpp音频文件
  • Config.hpp配置文件
  • GpuPreference.hpp GpuPreference.hpp
  • Graphics.hpp图形.hpp
  • Main.hpp主文件
  • Network.hpp网络.hpp
  • OpenGL.hpp OpenGL.hpp
  • System.hpp系统文件
  • Window.hpp窗口.hpp

And I have my main.cpp which is an example to see if my SMFL works:我有我的 main.cpp 这是一个例子,看看我的 SMFL 是否有效:

#include <Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(shape);
        window.display();
    }
    return 0;
}

The problem is my compiler says: " No such file or directory " with #include <SFML/Window.hpp> in red.问题是我的编译器说:“没有这样的文件或目录”, #include <SFML/Window.hpp>为红色。

I tried to find a solution through VisualStudioCode with the IncludePaths inside my .json on each file .hpp but it doesn't work.我试图找到通过VisualStudioCode我对每个文件.HPP以.json内的includepaths一个解决方案,但它不工作。

I also tried on my terminal to put a command: g++ -I/SFML/include main.cpp -o EX but I have the error above.我还尝试在我的终端上放置一个命令: g++ -I/SFML/include main.cpp -o EX但我有上面的错误。

Or: g++ -I/SFML/include/** main.cpp -o EX but this time it's #include <SFML/System.hpp> which is in red.或者: g++ -I/SFML/include/** main.cpp -o EX 但这次是#include <SFML/System.hpp>红色。

I don't understand why he put this error because I don't include it... It seems to not recognize my paths or to have a dependency between the .hpp.我不明白他为什么会出现这个错误,因为我没有包含它......它似乎无法识别我的路径或在 .hpp 之间存在依赖关系。 Maybe the solution is inside the directory "lib" but I don't know how to specify it or to manipulate it.也许解决方案在目录“lib”中,但我不知道如何指定或操作它。

I'm blocked since yesterday and I hope you will help me.我从昨天起就被屏蔽了,希望你能帮助我。

Christophe.克里斯托夫。

if i was you, i wouldn't bother making a makefile myself.如果我是你,我不会费心自己制作一个makefile。

Instead i will use cmake to generate the makefile and let him compile both my code and SFML.相反,我将使用 cmake 生成 makefile 并让他编译我的代码和 SFML。

If you want to do it like that, you can just download cmake on their website.如果你想这样做,你可以在他们的网站上下载 cmake。

  • https://cmake.org/ don't forget to add cmake into your path variable if you are on window https://cmake.org/如果您在窗口上,请不要忘记将 cmake 添加到您的路径变量中

then in your project create a file named然后在您的项目中创建一个名为

  • CMakeLists.txt CMakeLists.txt
  • A Lib folder where you have all your lib一个 Lib 文件夹,您可以在其中拥有所有的 lib
  • A Source folder where you have all your source file一个包含所有源文件的源文件夹
  • A Build folder where the compilation result will be编译结果所在的 Build 文件夹

in your CMakeLists copy and paste that code for sfml with c++11在您的 CMakeLists 中,使用 c++11 复制并粘贴 sfml 的代码

cmake_minimum_required(VERSION 3.19)
project(ProjectNAME)

# Source files
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/source")
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/lib")

#All the sources files listed
#List only the cpp file, header are automatically listed
set(SOURCES
        "${SRC_DIR}/main.cpp"
        )


# Executable definition and properties
add_executable(${PROJECT_NAME} ${SOURCES})
target_include_directories(${PROJECT_NAME} PRIVATE "${SRC_DIR}")
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)

# SFML
set(SFML_DIR "${LIB_DIR}/SFML")

# this line is here to avoid having to move the dll into the build folder
set(BUILD_SHARED_LIBS FALSE)


add_subdirectory("${SFML_DIR}")
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-audio sfml-system sfml-window sfml-network)

Don't forget to put your main.cpp in the source folder and your SFML folder in the Lib Folder.不要忘记将 main.cpp 放在源文件夹中,将 SFML 文件夹放在 Lib 文件夹中。 Pay attention to name correctly all the folder in the CMakeLists.txt注意正确命名CMakeLists.txt中的所有文件夹

then if you are on linux with gcc or g++:那么如果你在使用 gcc 或 g++ 的 linux 上:

  • open your terminal打开你的终端
  • go into the Build folder进入构建文件夹
  • execute the command " cmake .. "执行命令“ cmake ..
  • wait till it finish等到它完成
  • execute the command " make "执行命令“ make
  • wait till it finish等到它完成
  • launch your program with " ./ProjectNAME "使用“ ./ProjectNAME ”启动您的程序

if you are on window with mingw (don't forget to add mingw in your path):如果您在使用 mingw 的窗口(不要忘记在您的路径中添加 mingw):

  • open your terminal打开你的终端
  • go into the root folder进入根文件夹
  • execute the command " cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -B build/Debug -DCMAKE_BUILD_TYPE=Debug "执行命令“ cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -B build/Debug -DCMAKE_BUILD_TYPE=Debug
  • wait till it finish等到它完成
  • go into the build folder进入构建文件夹
  • execute the command " mingw32-make -j "执行命令“ mingw32-make -j
  • wait till it finish等到它完成
  • launch your program with " ProjectNAME.exe "使用“ ProjectNAME.exe ”启动您的程序

And everything should work easily, without having to configure the make file yourself.一切都应该很容易工作,而不必自己配置 make 文件。 And if you want to learn more about cmake they have a great documentation如果您想了解有关 cmake 的更多信息,他们有很棒的文档

Now you just have to include <SFML/graphics.hpp>现在你只需要包含 <SFML/graphics.hpp>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM