Mac M1,VScode,g++编译使用glut包的c++文件
#include <GLUT/glut.h>
void Initial()
{
glEnable(GL_DEPTH_TEST);//启用深度测试
glClearColor(1.0f,1.0f,1.0f,1.0f);//背景为白色
}
void ChangeSize(int w,int h)
{
if(h==0)
h=1;
glViewport(0,0,w,h); //设置视区尺寸
glMatrixMode(GL_PROJECTION); //制定当前操作投影矩阵堆栈
glLoadIdentity(); //重置投影矩阵
GLfloat fAspect;
fAspect=(float)w/(float)h; //计算视区宽高比
gluPerspective(45.0,fAspect,1.0,500.0); //指定透视投影的观察空间
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Display(void)
{
static float fElect1=0.0f; //绕原子旋转的角度
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //清除颜色和深度缓冲区
glMatrixMode(GL_MODELVIEW); //指定当前操作模型视图矩阵堆栈
glLoadIdentity(); //重置模型视图矩阵
glTranslatef(0.0f,0.0f,-250.0f); //将图形沿着z轴负向移动
glColor3f(1.0f,0.0f,0.0f);
glutSolidSphere(12.0f,15,15); //绘制红色的原子
glColor3f(0.0f,0.0f,0.0f);
glPushMatrix(); //保存当前模型视图矩阵
glRotatef(fElect1,0.0f,1.0f,0.0f); //绕y轴旋转一定的角度
glTranslatef(90.0f,0.0f,0.0f); //平移一段距离
glutSolidSphere(6.0f,15,15); //画出第一个电子
glPopMatrix(); //恢复模型视图矩阵
glPushMatrix(); //保存当前模型视图矩阵
glRotatef(45.0f,0.0f,0.0f,1.0f); //绕z轴旋转45度
glRotatef(fElect1,0.0f,1.0f,0.0f);
glTranslatef(-70.0f,0.0f,0.0f);
glutSolidSphere(6.0f,15,15); //画出第二个电子
glPopMatrix(); //恢复模型视图矩阵
glPushMatrix(); //保存当前模型视图矩阵
glRotatef(-45.0f,0.0f,0.0f,1.0f); //绕z轴旋转-45度
glRotatef(fElect1,0.0f,1.0f,0.0f);
glTranslatef(0.0f,0.0f,60.0f);
glutSolidSphere(6.0f,15,15); //画出第三个电子
glPopMatrix();
fElect1+=10.0f; //增加旋转步长,产生动画效果
if(fElect1>360.0f) fElect1=10.0f;
glutSwapBuffers();
}
void TimerFunc(int value)
{
glutPostRedisplay();
glutTimerFunc(100,TimerFunc,1); //100毫秒后调用定时器回调函数
}
int main(int argc,char* argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH);//窗口使用RGB颜色,双缓存和深度缓存
glutCreateWindow("分子动画示例");
glutReshapeFunc(ChangeSize);
glutDisplayFunc(Display);
glutTimerFunc(500,TimerFunc,1); //指定定时器回调函数
Initial();
glutMainLoop();
return 0;
}
(base) yuwenshuo@Cosmos-Computer molecule % pwd
/Users/yuwenshuo/Programming/cpp_VS/molecule
(base) yuwenshuo@Cosmos-Computer molecule % ls
molecule.cpp
(base) yuwenshuo@Cosmos-Computer molecule % g++ -std=c++11 molecule.cpp `pkg-config gtkmm-3.0 --cflags --libs`
clang: error: no such file or directory: 'molecule.cpp'
(base) yuwenshuo@Cosmos-Computer molecule %
直接run Code,命令行g++ 文件名 -o均不可行,且快捷键无法补全文件名。
此外拆分成两个文件main.cpp和molecule.h运行结果如下
(base) yuwenshuo@Cosmos-Computer molecule % g++ -g main.cpp -lstdc++ -o main
Undefined symbols for architecture arm64:
"_glClear", referenced from:
Display() in main-3acb69.o
"_glClearColor", referenced from:
Initial() in main-3acb69.o
"_glColor3f", referenced from:
Display() in main-3acb69.o
"_glEnable", referenced from:
Initial() in main-3acb69.o
"_glLoadIdentity", referenced from:
ChangeSize(int, int) in main-3acb69.o
Display() in main-3acb69.o
"_glMatrixMode", referenced from:
ChangeSize(int, int) in main-3acb69.o
Display() in main-3acb69.o
"_glPopMatrix", referenced from:
Display() in main-3acb69.o
"_glPushMatrix", referenced from:
Display() in main-3acb69.o
"_glRotatef", referenced from:
Display() in main-3acb69.o
"_glTranslatef", referenced from:
Display() in main-3acb69.o
"_glViewport", referenced from:
ChangeSize(int, int) in main-3acb69.o
"_gluPerspective", referenced from:
ChangeSize(int, int) in main-3acb69.o
"_glutCreateWindow", referenced from:
_main in main-3acb69.o
"_glutDisplayFunc", referenced from:
_main in main-3acb69.o
"_glutInit", referenced from:
_main in main-3acb69.o
"_glutInitDisplayMode", referenced from:
_main in main-3acb69.o
"_glutMainLoop", referenced from:
_main in main-3acb69.o
"_glutPostRedisplay", referenced from:
TimerFunc(int) in main-3acb69.o
"_glutReshapeFunc", referenced from:
_main in main-3acb69.o
"_glutSolidSphere", referenced from:
Display() in main-3acb69.o
"_glutSwapBuffers", referenced from:
Display() in main-3acb69.o
"_glutTimerFunc", referenced from:
TimerFunc(int) in main-3acb69.o
_main in main-3acb69.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
头文件已使用ifdefine包裹,glut引用路径正常,但是仍然存在以上问题
(base) yuwenshuo@Cosmos-Computer molecule % gcc -v
Using built-in specs.
COLLECT_GCC=gcc-12
COLLECT_LTO_WRAPPER=/opt/homebrew/Cellar/gcc/12.2.0/bin/../libexec/gcc/aarch64-apple-darwin22/12/lto-wrapper
Target: aarch64-apple-darwin22
Configured with: ../configure --prefix=/opt/homebrew/opt/gcc --libdir=/opt/homebrew/opt/gcc/lib/gcc/current --disable-nls --enable-checking=release --with-gcc-major-version-only --enable-languages=c,c++,objc,obj-c++,fortran --program-suffix=-12 --with-gmp=/opt/homebrew/opt/gmp --with-mpfr=/opt/homebrew/opt/mpfr --with-mpc=/opt/homebrew/opt/libmpc --with-isl=/opt/homebrew/opt/isl --with-zstd=/opt/homebrew/opt/zstd --with-pkgversion='Homebrew GCC 12.2.0' --with-bugurl=https://github.com/Homebrew/homebrew-core/issues --with-system-zlib --build=aarch64-apple-darwin22 --with-sysroot=/Library/Developer/CommandLineTools/SDKs/MacOSX13.sdk
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 12.2.0 (Homebrew GCC 12.2.0)
(base) yuwenshuo@Cosmos-Computer molecule % g++ -g molecule.cpp -lstdc++ -o main
cc1plus: fatal error: molecule.cpp: No such file or directory
compilation terminated.
并且更换到hello world所在文件中后,仍然提示
ld: symbol(s) not found for architecture arm64
collect2: error: ld return 1 exit status
直接run Code,命令行g++ 文件名 -o均不可行,且快捷键无法补全文件名。
你用一个最简单的 hello-world 的程序,运行一下,确保没有问题, 然后换上你自己的代码,看看第一个错误发生在哪里。
你没有安装arm64 cpu架构的glut库
你用的好像是clang编译器