C ++错误:对“”的未定义引用,并且ld返回了1个退出状态[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C ++错误:对“”的未定义引用,并且ld返回了1个退出状态[重复]相关的知识,希望对你有一定的参考价值。
我创建了一个OpenGL项目来绘制形状。但是,当我尝试编译程序时,出现一些错误。这是Shapes.h的内容:
#ifndef SHAPES_INCLUDE
#define SHAPES_INCLUDE
void Triangle(int x1, int y1, int x2, int y2, int x3, int y3 , float r, float g, float b);
#endif
这里是“ Shapes.cpp”的内容:
#include "Shapes.h"
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glut.h>
void Triangle(int x1, int y1, int x2, int y2, int x3, int y3 , float r, float g, float b)
{
glColor3f(r,g,b);
glBegin(GL_TRIANGLES);
glVertex2i(x1,y1);
glVertex2i(x2,y2);
glVertex2i(x3,y3);
glEnd();
}
供参考,这是我的“ main.cpp”的内容:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <GL/glx.h>
#include <GL/gl.h>
#include <GL/glut.h>
// our libraries:
#include "libraries/Shapes.h"
void init()
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,200.0,0.0,150.0);
}
void lineSegment()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glVertex2i(20,120);
glVertex2i(40,20);
glVertex2i(80,20);
glEnd();
glFlush();
}
void glLoop()
{
Triangle(20,120,40,20,80,20,0.0,0.0,1.0);
glFlush();
}
int main(int argc, char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50,100);
glutInitWindowSize(400,300);
glutCreateWindow("OpenGL window");
init();
glutDisplayFunc(glLoop);
glutMainLoop();
return 0;
}
并且当我用g++ main.cpp -lGL -lGLU -lglut
编译时,出现此错误:
/tmp/ccSGyfe8.o: In function `glLoop()':
main.cpp:(.text+0xf3): undefined reference to `Triangle(int, int, int, int, int, int, float, float, float)'
collect2: error: ld returned 1 exit status
有人可以帮我修复它吗?
答案
您不链接包含Triangle
定义的目标文件。以上是关于C ++错误:对“”的未定义引用,并且ld返回了1个退出状态[重复]的主要内容,如果未能解决你的问题,请参考以下文章