如何在macOS上使用Makefile?
How to use Makefile on macOS?
在macOS上,如果我用
clang -I. -framework OpenGL main.cpp glad.c /usr/local/Cellar/glfw/3.3.1/lib/libglfw.3.3.dylib
我可以从以下代码构建并运行生成的可执行文件:
#define GL_SILENCE_DEPRECATION
#include <stdio.h>
#include "glad/glad.h"
#include "/usr/local/Cellar/glfw/3.3.1/include/GLFW/glfw3.h"
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClearColor(0.9, 0.1, 0.1, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
但是我想使用 Makefile 进行构建,所以我准备了
CC = clang
XXFLAGS = -g -Wall -v
CPPFLAGS = -I.
LDLIBS = -lm -L/usr/local/Cellar/glfw/3.3.1/lib -lglfw
LDFLAGS = -framework OpenGL
OBJS = main.o glad.o
PROGRAM = main
$(PROGRAM): $(OBJS)
$(CXX) $^ $(XXFLAGS) $(CPPFLAGS) $(LDLIBS) $(LDFLAGS) -o $@
clean:
-rm -f *.o
distclean: clean
-rm -Rf $(PROGRAM)
它实际上使可执行文件,但是当我运行它时,我得到
$ ./main
Segmentation fault: 11
所以我想 Makefile 有问题,对吧?
$ make -n
c++ -I. -c -o main.o main.cpp
clang -I. -c -o glad.o glad.c
c++ main.o glad.o -g -Wall -v -I. -lm -L/usr/local/Cellar/glfw/3.3.1/lib -lglfw -framework OpenGL -o main
您没有定义规则来构建OBSJ
,因此make
使用了其内置规则。PROGRAM
规则仅链接对象文件,没有必要在那里指定CPPFLAGS
。
.PHONNY: all
all: clean a.out
a.out:
clang++ -I glad/include -F /Library/frameworks -framework OpenGL main.cpp glad/src/glad.c /usr/local/Cellar/glfw/3.3.1/lib/libglfw.3.3.dylib
clean:
rm a.out
引用:
- https://www.gnu.org/software/make/manual/make.html
- https://opensource.com/article/18/8/what-how-makefile
相关文章:
- node-gyp 在 macOS 上未正确链接库
- 用MacOS Mojave编译C++:致命错误:mpi.h:没有这样的文件或目录
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- MacOS通过在莫哈韦"wchar.h"下破碎的自制啤酒发出叮当声
- 从对象文件(.o)在macos上制作归档文件(.a文件)
- 对于MacOS上的G++,如何添加默认的include目录/usr/local/include和默认的库搜索路径/usr
- 针对遗留库的链接:来自预制makefile的-lgfortranbegin
- macOS 上的 openvino 构建问题
- 使用 Makefile 中的头文件编译 Pybind (不使用 cmake)
- 为什么 __gcd() 在 macOS mojave 中抛出错误?
- 如何在 macOS 卡塔琳娜上解决此错误?
- 如何在使用Makefile构建后在cmd中运行该程序?
- Makefile g++ 使用命令行中的 -D 变量进行编译,默认值
- 在macOS Mojave上发布BOOST unit_test_framework/Google测试
- 在 macOS 上安装 clang_complete
- @CPPFLAGS@在 Makefile.in 中意味着什么?
- 从macOS Catalina上的源代码编译LLVM(如何解决未找到"stdio.h")
- 在macOS Mojave上尝试OpenCV视频捕获时"Abort Trap: 6"
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如何在macOS上使用Makefile?