当我制作这些文件时,g ++会发生两个错误,即多重定义和先前定义。
生成文件:
INCLUDE = -I/usr/X11R6/include/
LIBDIR = -L/usr/X11R6/lib
FLAGS = -Wall
CC = g++
CFLAGS = $(FLAGS) $(INCLUDE)
LIBS = -lglut -lGL -lGLU
glTestDemo.o: glTestDemo.cpp headers.h
$(CC) $(CFLAGS) -c glTestDemo.cpp
display.o: display.cpp headers.h
$(CC) $(CLFAGS) -c display.cpp
glTestDemo: glTestDemo.o display.o
$(CC) $(CFLAGS) glTestDemo.o display.o -o $@ $(LIBDIR) $< $(LIBS) # The initial white space is a tab
all: glTestDemo
clean:
rm glTestDemo *.o
头文件.h
#ifndef __HEADERS_H__
#define __HEADERS_H__
#include <GL/glut.h>
extern int NumPoints;
extern void incorrect_display (void);
#endif
显示.cpp
#include "headers.h"
void
incorrect_display (void)
{
glClear (GL_COLOR_BUFFER_BIT);
glPointSize (1.0);
glDrawArrays (GL_POINTS, 0, NumPoints);
glFlush ();
}
glTestDemo.cpp
#include "headers.h"
int NumPoints = 5000;
int
main (int argc, char** argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_RGBA);
glutInitWindowPosition (50, 50);
glutInitWindowSize (600, 600);
glutCreateWindow ("Test title");
glutDisplayFunc (incorrect_display);
glutMainLoop ();
return 0;
}
在我输入 make all 后出现这样的错误消息:
/usr/bin/ld: error: glTestDemo.o: multiple definition of 'NumPoints'
/usr/bin/ld: glTestDemo.o: previous definition here
/usr/bin/ld: error: glTestDemo.o: multiple definition of 'main'
/usr/bin/ld: glTestDemo.o: previous definition here
collect2: error: ld returned 1 exit status
make: *** [glTestDemo] Error 1
我制作了一个 shell 脚本来测试我的 c++ 语法和 g++ 标志和 openGL 标志,它们通过链接使用。这是成功。因此,我认为是 Makefile 导致了错误。但我找不到这个问题。