为什么不g++使用 -I 选项?

发布于 2025-01-03 06:09:59 字数 1967 浏览 1 评论 0原文

我正在尝试在 Linux 上分发一个使用 openGL 和 SFML 用 C++ 编写的小软件。我试图在我的代码中提供 sfml 库和标头,以避免安装 sfml(使用它的人没有 root 访问权限,但他们将安装所有 openGL 所需的东西)。

我的文件层次结构如下:

  • lib/>>这里是所有 sfml 库(.so 文件)
  • Makefile >>>后来我显示代码
  • src/ >>>这是程序和 sfml 源代码
    • myfiles.h、myfiles.cpp >>>所有这些都可以与安装在 /usr/lib 的 sfml 库一起编译并正常工作
    • SFML/ >>>这里是所有 sfml 标头,有一些子文件夹

这是我的 Makefile:

EJECUTABLE = app

MODULOS = src/main.o src/Handbapp.o src/Camera.o src/Light.o src/Scene.o src/Graphics.o src/Window.o src/Model.o src/Court.o src/Player.o src/Primitives.o src/Path.o

CC = g++
LIBDIR = ./lib
INCDIR = ./src

LIBS = -lsfml-window -lsfml-system -lGLU

LDFLAGS = -L$(LIBDIR) -I$(INCDIR)
CFLAGS = -v -Wl,-rpath,$(LIBDIR)

$(EJECUTABLE): clean $(MODULOS)
    $(CC) $(CFLAGS) -o $(EJECUTABLE) $(LDFLAGS) $(MODULOS) $(LIBS)
    rm -f $(MODULOS) 

clean:
    rm -f $(MODULOS) $(EJECUTABLE)

当我在 /usr/lib 安装了 sfml 的 PC (Ubuntu 11.10) 中运行 make 时,如果我在没有安装它的电脑中运行 make ,一切都会顺利它说:

...
g++    -c -o src/main.o src/main.cpp
In file included from src/main.h:18:0,
                 from src/main.cpp:10:
src/Handbapp.h:17:44: fatal error: SFML/Window.hpp: File or directory doesn't exist
Compilation finished.
make: *** [src/main.o] Error 1

这是一段显示 Handbapp.h 上包含内容的代码:

...
#ifndef HANDBAPP_H
#define HANDBAPP_H

// Espacio de nombres
using namespace std;

// Librerias
#include <GL/gl.h> // OpenGL
#include <GL/glu.h> // Utilidades OpenGL
#include <SFML/Window.hpp> // Ventanas SFML <- LINE 17
#include <SFML/System.hpp> // SFML

我尝试制作#include“whatever/Window.hpp”,更改 src/SFML文件夹名称,并且不使用链接器上的 -I 选项,但 src/SFML/Window.hpp (和其他 sfml 标头)包含类似 #include 的行SFML/Window/Whatever.hpp >,所以我需要它们在我指定的路径中搜索。

我错过了什么吗?我猜这是我的 Makefile 的错误,但我对此没有太多经验...

提前致谢!

I'm trying to distribute a little software written on C++ on Linux that uses openGL and SFML. I'm trying to provide the sfml libraries and headers with my code in order to avoid the installation of sfml (people who will use it have no root access, but they will have all the openGL needed stuff installed).

My file hierarchy is as follows:

  • lib/ >> here goes all the sfml libraries (.so files)
  • Makefile >> later I show the code
  • src/ >> Here goes program and sfml sources
    • myfiles.h, myfiles.cpp >> all them compile and work allright with the sfml libraries installed at /usr/lib
    • SFML/ >> here goes all the sfml headers, has some subfolders

Here's my Makefile:

EJECUTABLE = app

MODULOS = src/main.o src/Handbapp.o src/Camera.o src/Light.o src/Scene.o src/Graphics.o src/Window.o src/Model.o src/Court.o src/Player.o src/Primitives.o src/Path.o

CC = g++
LIBDIR = ./lib
INCDIR = ./src

LIBS = -lsfml-window -lsfml-system -lGLU

LDFLAGS = -L$(LIBDIR) -I$(INCDIR)
CFLAGS = -v -Wl,-rpath,$(LIBDIR)

$(EJECUTABLE): clean $(MODULOS)
    $(CC) $(CFLAGS) -o $(EJECUTABLE) $(LDFLAGS) $(MODULOS) $(LIBS)
    rm -f $(MODULOS) 

clean:
    rm -f $(MODULOS) $(EJECUTABLE)

When I run make in a PC (Ubuntu 11.10) with sfml installed in /usr/lib it all goes well, if I do in one that doesn't have it installed it says:

...
g++    -c -o src/main.o src/main.cpp
In file included from src/main.h:18:0,
                 from src/main.cpp:10:
src/Handbapp.h:17:44: fatal error: SFML/Window.hpp: File or directory doesn't exist
Compilation finished.
make: *** [src/main.o] Error 1

Here's a piece of code showing the include on Handbapp.h:

...
#ifndef HANDBAPP_H
#define HANDBAPP_H

// Espacio de nombres
using namespace std;

// Librerias
#include <GL/gl.h> // OpenGL
#include <GL/glu.h> // Utilidades OpenGL
#include <SFML/Window.hpp> // Ventanas SFML <- LINE 17
#include <SFML/System.hpp> // SFML

I've tried making #include "whatever/Window.hpp",changing src/SFML folder name for whatever and not using the -I option on the linker, but src/SFML/Window.hpp (and other sfml headers) have include lines like that #include < SFML/Window/Whatever.hpp >, so I need them to search at the path I specify.

Am I missing something? I guess it's an error with my Makefile, but I don't have so much experience with it...

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜警司 2025-01-10 06:09:59

您需要将 -I$(INCDIR) 放入 CPPFLAGS,而不是 LDFLAGS。然后,它将被内置规则拾取以编译各个目标文件。

您还应该将 CFLAGS 重命名为 CXXFLAGS,将 CC 重命名为 CXXCCCFLAGS 适用于 C 源文件,而不是 C++ 源文件。

您不应该$(EJECUTABLE)依赖于clean,并且您不应该执行rm -f $(MODULOS)链接命令后的代码>。这些东西违背了 Makefile 的目的,即只重新编译必要的部分,而不是每次都重新编译整个程序。

You need to put -I$(INCDIR) into CPPFLAGS, not LDFLAGS. Then it will be picked up by the built-in rule for compilation of individual object files.

You should also rename CFLAGS to CXXFLAGS and CC to CXX. CC and CFLAGS are for C source files, not C++ source files.

You should not have $(EJECUTABLE) depend on clean, and you should not execute rm -f $(MODULOS) after the link command. Those things defeat the purpose of a Makefile, which is to recompile only what is necessary, not the whole program every single time.

幻想少年梦 2025-01-10 06:09:59

问题不在编译器中,而在您的 Makefile 中:您想要在适当的标志中设置包含目录路径(我通常使用 CPPFLAGS ,但我通常也有自己的规则,明确引用我正在使用的标志)。 LDFLAGS 肯定只传递到构建的链接阶段。

The problem isn't in the compiler but in your Makefile: you want to set up the include directory path in the appropriate flags (I typically use CPPFLAGS but I typically also have my own rules which explicitly reference the flags I'm using). The LDFLAGS are definitely only passed to the linking stage of the build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文