简体   繁体   English

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

[英]Why doesn't g++ use the -I option?

I'm trying to distribute a little software written on C++ on Linux that uses openGL and SFML. 我正在尝试在Linux上使用openGL和SFML分发一些用C ++编写的软件。 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). 我正在尝试使用我的代码提供sfml库和头文件以避免安装sfml(使用它的人没有root访问权限,但是他们将安装所有需要的openGL东西)。

My file hierarchy is as follows: 我的文件层次结构如下:

  • lib/ >> here goes all the sfml libraries (.so files) lib / >>这里是所有的sfml库(.so文件)
  • Makefile >> later I show the code Makefile >>稍后我会显示代码
  • src/ >> Here goes program and sfml sources src / >>这里有程序和sfml源代码
    • myfiles.h, myfiles.cpp >> all them compile and work allright with the sfml libraries installed at /usr/lib myfiles.h,myfiles.cpp >>所有这些都可以在/ usr / lib下安装的sfml库编译和工作
    • SFML/ >> here goes all the sfml headers, has some subfolders SFML / >>这里是所有sfml头文件,有一些子文件夹

Here's my Makefile: 这是我的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: 当我在一台PC(Ubuntu 11.10)中使用安装在/ usr / lib中的sfml运行make时,一切顺利,如果我在没有安装sfml的那个中运行它说:

...
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: 这是一段显示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. 我已经尝试制作#include“whatever / Window.hpp” ,更改src / SFML文件夹名称,而不是在链接器上使用-I选项,但是src / SFML / Window.hpp(和其他sfml头文件)包括这样的行#include <SFML / Window / Whatever.hpp> ,所以我需要它们在我指定的路径上搜索。

Am I missing something? 我错过了什么吗? I guess it's an error with my Makefile, but I don't have so much experience with it... 我猜我的Makefile是个错误,但我没有太多的经验...

Thanks in advance! 提前致谢!

You need to put -I$(INCDIR) into CPPFLAGS , not LDFLAGS . 您需要将-I$(INCDIR)放入CPPFLAGS ,而不是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 . 您还应将CFLAGS重命名为CXXFLAGS ,将CC重命名为CXX CC and CFLAGS are for C source files, not C++ source files. CCCFLAGS用于C源文件,而不是C ++源文件。

You should not have $(EJECUTABLE) depend on clean , and you should not execute rm -f $(MODULOS) after the link command. 应该有$(EJECUTABLE)取决于clean ,你不应该执行rm -f $(MODULOS)链接命令后。 Those things defeat the purpose of a Makefile, which is to recompile only what is necessary, not the whole program every single time. 这些东西违背了Makefile的目的,即只重新编译必要的内容,而不是每次重新编译整个程序。

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). 问题不在编译器中,而是在Makefile中:你想在适当的标志中设置include目录路径(我通常使用CPPFLAGS但我通常也有自己的规则明确引用我正在使用的标志)。 The LDFLAGS are definitely only passed to the linking stage of the build. LDFLAGS肯定只传递给构建的链接阶段。

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

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