C++ - 执行makefile时找不到目录
Posted
技术标签:
【中文标题】C++ - 执行makefile时找不到目录【英文标题】:C++ - No directory found when executing makefile 【发布时间】:2020-05-08 11:37:44 【问题描述】:操作系统:Windows 10 - 编译器:mingw64
我有一个使用 OpenGL 的非常简单的程序。我唯一的文件 main.cpp
如下:
/*
* OGL02Animation.cpp: 3D Shapes with animation
*/
//#include <windows.h> // for MS Windows
#include <GL/glut.h> // GLUT, include glu.h and gl.h
/* Global variables */
char title[] = "3D Shapes with animation";
GLfloat anglePyramid = 0.0f; // Rotational angle for pyramid [NEW]
GLfloat angleCube = 0.0f; // Rotational angle for cube [NEW]
int refreshMills = 15; // refresh interval in milliseconds [NEW]
/* Initialize OpenGL Graphics */
void initGL()
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClearDepth(1.0f); // Set background depth to farthest
glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
glDepthFunc(GL_LEQUAL); // Set the type of depth-test
glShadeModel(GL_SMOOTH); // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void display()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW); // To operate on model-view matrix
// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f); // Move right and into the screen
glRotatef(angleCube, 1.0f, 1.0f, 1.0f); // Rotate about (1,1,1)-axis [NEW]
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f( 1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
// Render a pyramid consists of 4 triangles
glLoadIdentity(); // Reset the model-view matrix
glTranslatef(-1.5f, 0.0f, -6.0f); // Move left and into the screen
glRotatef(anglePyramid, 1.0f, 1.0f, 0.0f); // Rotate about the (1,1,0)-axis [NEW]
glBegin(GL_TRIANGLES); // Begin drawing the pyramid with 4 triangles
// Front
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f); // Green
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd(); // Done drawing the pyramid
glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
// Update the rotational angle after each refresh [NEW]
anglePyramid += 0.2f;
angleCube -= 0.15f;
/* Called back when timer expired [NEW] */
void timer(int value)
glutPostRedisplay(); // Post re-paint request to activate display()
glutTimerFunc(refreshMills, timer, 0); // next timer call milliseconds later
/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv)
glutInit(&argc, argv); // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480); // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL(); // Our own OpenGL initialization
glutTimerFunc(0, timer, 0); // First timer call immediately [NEW]
glutMainLoop(); // Enter the infinite event-processing loop
return 0;
这是我创建的makefile
:
# Makefile
INCLUDE = -I"C:\Program Files\mingw-w64\mingw64\include"
LIBRARIES = -L"C:\Program Files\mingw-w64\mingw64\lib" -lGL -lGLU -lglut -lm -lXi -lXmu -lglu32 -lopengl32 -lfreeglut
COMPILER = g++ --std=c++17
COMPILERFLAGS = -no-pie $(INCLUDE)
PROGRAM = test
SOURCE = main.cpp
OBJECT = main.o
.cc.o: $(SOURCE)
$(COMPILER) -c $(COMPILERFLAGS) $<
all: $(PROGRAM)
$(PROGRAM): $(OBJECT) $(SOURCE)
$(COMPILER) $(COMPILERFLAGS) -o $(PROGRAM) $(OBJECT) $(LIBRARIES)
clean:
-rm -rf core *.o *~ .*~ $(PROGRAM)
作为 IDE,我正在使用 CodeBlocks。当我在 IDE 上编译和运行程序时,它运行良好。另外,当我在命令行中构建和运行程序时,它也运行良好: p>
g++ main.cpp -o output.exe -I"C:\Program Files\mingw-w64\mingw64\include" -lglu32 -lopengl32 -lfreeglut
当我使用 gnu 命令make
时,唯一的问题来了。当我在cmd
中执行make test
时,出现以下错误:
g++ -c -o main.o main.cpp main.cpp:5:10:致命错误:GL/glut.h:否 这样的文件或目录 #include // GLUT,包括 glu.h 和gl.h ^~~~~~~~~~~ 编译终止。 make: *** [: main.o] 错误 1
库和路径在makefile
中是否定义得不好?我无法完全理解为什么make
不起作用。
谢谢。
【问题讨论】:
您没有为 GL 添加包含路径。 GL/glut.h的路径是什么? C:\Program Files\mingw-w64\mingw64\include\GL 我在你的makefile中找不到g++ -c -o main.o main.cpp
这一行?你确定你使用了正确的 Makefile 吗?
应该是这个:$(PROGRAM): $(OBJECT) $(SOURCE) $(COMPILER) $(COMPILERFLAGS) -o $(PROGRAM) $(OBJECT) $(LIBRARIES)
,对吧?
这一行的输出应该是g++ --std=c++17 -no-pie -I"C:\Program Files\mingw-w64\mingw64\include" -o test -L"C:\Program Files\mingw-w64\mingw64\lib" -lGL -lGLU -lglut -lm -lXi -lXmu -lglu32 -lopengl32 -lfreeglut
。但实际输出是g++ -c -o main.o main.cpp
。目前您正在使用不同的 makefile。给定的 Makefile 无法生成您的错误消息。 Makefile 必须准确命名为 Makefile
,大写 M
。或者你必须打电话给make -f makefile
【参考方案1】:
您定义了一个规则.cc.o
,它使用您自己的变量名称定义编译。
但是,您的文件名称为main.cpp
,因此它将使用默认规则:
.cpp.o:
$(COMPILE.cpp) $(OUTPUT_OPTION) $<
具有以下默认定义:
COMPILE.cpp = $(COMPILE.cc)
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c
OUTPUT_OPTION = -o $@
所以要么将main.cpp
的文件名更改为main.cc
,COMPILE.cpp
的定义,或者.cpp.o
目标的定义。或者,您可以将标志添加到默认变量名称并删除 .cc.o 规则。
【讨论】:
以上是关于C++ - 执行makefile时找不到目录的主要内容,如果未能解决你的问题,请参考以下文章
C++ 可执行文件在运行时找不到库,即使它位于 /usr/lib 中(在 Linux 上)
在travis CI上运行Makefile时找不到gcc命令