在Windows上编译和运行示例C ++程序的几个问题

时间:2012-01-04 05:24:34

标签: c++ windows

正如您所看到的,我对这个网站非常陌生。我一直在寻找像我一样的问题,但无济于事。

我的问题是,我无法编译c ++程序(link here注意:找到源代码here),并且找不到修复编译器错误的正确方法。

我尝试了几种编译方式:

1)使用cygwin。

我使用提供的makefile进行编译并尝试运行该文件(我在包含库(GLEW,GLUT,GLM)时遇到了很多问题,但是在将库放入cygwin目录后我修复了它们。然后它给了我一个freeglut无法打开显示错误,我修复了进入xcygwin。这给了我一个最后的错误,称为缺少GL版本。其他网站上的人说过在glew之前发起过剩,但我找不到init glew在哪里运行。我没有发现cygwin问题的消息。

2)通过CMD.exe使用g ++编译器

我读过你可以通过命令提示符编译C ++,所以我试着这样做。 我运行了命令g ++ glescraft.cpp noise.h noise.cpp textures.c -o glescraft,但也给出了无法找到GLEW的错误。我通过将库放在正确的位置再次修复了这些问题。在修复了所有库问题之后,它引发了许多关于多个声明的问题,以及对GL,GLEW和GLUT命令的未定义引用。如果你们需要他们的话,我可以发布错误。但是,我不相信文件中存在编程错误,因为在GNU / Linux上使用make编译的程序非常好,但由于我自己在nVIDIA驱动程序上遇到很多问题而无法测试(X没有启动等等)等等)。我在gnu / linux上尝试了g ++编译,但是相同的多个定义和未定义的引用。我在这些上找不到任何东西。

第3)的代码::块

完全相同的事情是使用g ++编译器,因为我相信code :: blocks也使用了mingw g ++编译器。

顺便说一下,当我说我在命令提示符下尝试了g ++时,我也尝试了gcc,但也有同样的错误。

我的问题是,我怎样才能编译这个程序,尽可能少依赖?

你们能找到办法吗?我很难过,根本找不到办法,我已经尝试了一个星期左右。

编辑:正如一个人所指出的,这里是错误:

在cmd中使用g ++编译器后显示的错误,也与code :: blocks中显示的错误相同:

C:\prog\glescraft>g++ glescraft.cpp noise.h noise.cpp textures.c -o glescraft.ex
e
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x0): multiple d
efinition of `linearInterpolation(float, float, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x58): first
 defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x11): multiple
definition of `biLinearInterpolation(float, float, float, float, float, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x69): first
 defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x76): multiple
definition of `triLinearInterpolation(float, float, float, float, float, float,
float, float, float, float, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xce): first
 defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x111): multiple
 definition of `noise2d(int, int, int)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x169): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x1e7): multiple
 definition of `noise3d(int, int, int, int)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x23f): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x2f4): multiple
 definition of `noise2d_gradient(float, float, int)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x34c): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x454): multiple
 definition of `noise3d_gradient(float, float, float, int)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x4ac): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x6e0): multiple
 definition of `noise2d_perlin(float, float, int, int, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x738): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x765): multiple
 definition of `noise2d_perlin_abs(float, float, int, int, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x7bd): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x7f6): multiple
 definition of `noise3d_perlin(float, float, float, int, int, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x84e): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccCAHbwO.o:noise.cpp:(.text+0x885): multiple
 definition of `noise3d_perlin_abs(float, float, float, int, int, float)'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x8dd): firs
t defined here
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1c): undef
ined reference to `__glutInitWithExit'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x37): undef
ined reference to `__glutCreateWindowWithExit'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x52): undef
ined reference to `__glutCreateMenuWithExit'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xa9e): unde
fined reference to `_imp____glewIsShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xab9): unde
fined reference to `_imp____glewGetShaderiv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xadc): unde
fined reference to `_imp____glewIsProgram'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xaf7): unde
fined reference to `_imp____glewGetProgramiv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xb55): unde
fined reference to `_imp____glewIsShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xb70): unde
fined reference to `_imp____glewGetShaderInfoLog'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xb9a): unde
fined reference to `_imp____glewIsProgram'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xbb5): unde
fined reference to `_imp____glewGetProgramInfoLog'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xc51): unde
fined reference to `_imp____glewCreateShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xc66): unde
fined reference to `_imp____glewShaderSource'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xc9a): unde
fined reference to `_imp____glewCompileShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xcb3): unde
fined reference to `_imp____glewGetShaderiv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xd05): unde
fined reference to `_imp____glewDeleteShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xe48): unde
fined reference to `glGetIntegerv@8'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xefc): unde
fined reference to `_imp____glewCreateProgram'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xf14): unde
fined reference to `_imp____glewAttachShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xf2f): unde
fined reference to `_imp____glewAttachShader'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xf4a): unde
fined reference to `_imp____glewLinkProgram'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xf5e): unde
fined reference to `_imp____glewGetProgramiv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0xfc1): unde
fined reference to `_imp____glewGetAttribLocation'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1025): und
efined reference to `_imp____glewGetUniformLocation'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1082): und
efined reference to `_imp____glewActiveTexture'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x10ae): und
efined reference to `glGenTextures@8'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x10c6): und
efined reference to `glBindTexture@8'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1119): und
efined reference to `glTexImage2D@36'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1121): und
efined reference to `_imp____glewGenerateMipmap'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1211): und
efined reference to `_imp____glewGenBuffers'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1244): und
efined reference to `_imp____glewBindBuffer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1260): und
efined reference to `_imp____glewBufferData'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x128d): und
efined reference to `_imp____glewGenBuffers'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x134e): und
efined reference to `glViewport@16'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x13a2): und
efined reference to `_imp____glewUseProgram'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x13b6): und
efined reference to `_imp____glewUniform1i'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x14f8): und
efined reference to `_imp____glewUniformMatrix4fv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1552): und
efined reference to `glClearColor@16'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1561): und
efined reference to `glClear@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1580): und
efined reference to `glTexParameteri@12'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x159f): und
efined reference to `glTexParameteri@12'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x15ae): und
efined reference to `glEnable@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x15bd): und
efined reference to `glEnable@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x15d6): und
efined reference to `glPolygonOffset@8'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x15e5): und
efined reference to `glEnable@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x15ed): und
efined reference to `_imp____glewEnableVertexAttribArray'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1666): und
efined reference to `glReadPixels@28'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1e72): und
efined reference to `glDisable@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1e81): und
efined reference to `glDisable@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1e89): und
efined reference to `_imp____glewUniformMatrix4fv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1ec0): und
efined reference to `_imp____glewBindBuffer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1edc): und
efined reference to `_imp____glewBufferData'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1f09): und
efined reference to `_imp____glewVertexAttribPointer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1f5c): und
efined reference to `glDrawArrays@12'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1f83): und
efined reference to `glDisable@4'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1fa7): und
efined reference to `_imp____glewUniformMatrix4fv'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1fde): und
efined reference to `_imp____glewBindBuffer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x1ffa): und
efined reference to `_imp____glewBufferData'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2027): und
efined reference to `_imp____glewVertexAttribPointer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x207a): und
efined reference to `glDrawArrays@12'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2082): und
efined reference to `glutSwapBuffers'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x22f4): und
efined reference to `glutGet'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x24d7): und
efined reference to `glutPostRedisplay'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2643): und
efined reference to `glutWarpPointer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2825): und
efined reference to `_imp____glewDeleteProgram'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2862): und
efined reference to `glutInitDisplayMode'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2876): und
efined reference to `glutInitWindowSize'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2887): und
efined reference to `_imp__glewInit'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x28a0): und
efined reference to `_imp__glewGetErrorString'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x28ce): und
efined reference to `_imp____GLEW_VERSION_2_0'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x296c): und
efined reference to `glutSetCursor'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2980): und
efined reference to `glutWarpPointer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x298c): und
efined reference to `glutDisplayFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x2998): und
efined reference to `glutReshapeFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29a4): und
efined reference to `glutIdleFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29b0): und
efined reference to `glutSpecialFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29bc): und
efined reference to `glutSpecialUpFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29c8): und
efined reference to `glutIdleFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29d4): und
efined reference to `glutPassiveMotionFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29e0): und
efined reference to `glutMotionFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29ec): und
efined reference to `glutMouseFunc'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text+0x29f1): und
efined reference to `glutMainLoop'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN5chunk6up
dateEv[chunk::update()]+0x237e): undefined reference to `_imp____glewGenBuffers'

C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN5chunk6up
dateEv[chunk::update()]+0x23e9): undefined reference to `_imp____glewBindBuffer'

C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN5chunk6up
dateEv[chunk::update()]+0x2409): undefined reference to `_imp____glewBufferData'

C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN5chunk6re
nderEv[chunk::render()]+0x3f): undefined reference to `_imp____glewBindBuffer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN5chunk6re
nderEv[chunk::render()]+0x5f): undefined reference to `_imp____glewVertexAttribP
ointer'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN5chunk6re
nderEv[chunk::render()]+0xb7): undefined reference to `glDrawArrays@12'
C:\Users\patrick\AppData\Local\Temp\ccL5KhgI.o:glescraft.cpp:(.text$_ZN10superch
unk6renderERKN3glm6detail7tmat4x4IfEE[superchunk::render(glm::detail::tmat4x4 const&)]+0x2c7): undefined reference to `_imp____glewUniformMatrix4fv'
collect2: ld returned 1 exit status

很抱歉格式化,我似乎无法让代码标签处理复制粘贴。

1 个答案:

答案 0 :(得分:0)

其中一些多重定义错误是因为文件glescraft.cpp包含noise.cpp和textures.c的 source 文件,您还尝试在命令行上编译。更改源代码以包含正确的头文件,或者只编译glescraft.cpp。

未定义引用的问题是因为您没有链接到OpenGL库。至少将“-lgl -lglut”添加到命令行。

您还需要告诉编译器(实际上是链接器)在哪里找到库。这是通过“-L”标志完成的。

我的建议之后的命令行应该类似于:

g++ glescraft.cpp -o glescraft.exe -L<path to gl and glut libs> -lgl -lglut

将“<path to gl and glut libs>”替换为库的实际路径。

此外,在编译时,您永远不应编译头文件(以“.h”结尾的文件)。相反,它们被编译器包含在#include "file"指令中。

您可以在GCC manual中看到所有标记。选择您拥有的版本(可以使用命令g++ --version找到。)