Makefile不能正常工作

Makefile不能正常工作

本文介绍了通过Homebrew的SDL Max OSX,Makefile不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过自制软件安装了SDL,并且如果我直接在终端中输入以下命令,它就可以与我的测试程序完美配合:

I have installed SDL through homebrew, and it works perfectly with my test program if I enter the following command directly in the terminal:

 g++ -O3 -g -Wall -Wextra -std=c++1y hello.cpp hello_main.cpp `sdl2-config --cflags --libs` -o hello

但是不幸的是,我写一个makefile的尝试(我肯定会需要一个)产生了不成功/无法解释的结果.我正在遵循此操作,但是我的配置不同/我没有指定Cocoa(我不需要),所以我希望遇到的问题可能部分是由于我的不同要求造成的:在OS X上使用makefile编译SDL 示例:

but unfortunately my attempts to write a makefile (I will definitely need one) have yielded unsuccessful/unexplained results.I am following this, but my configuration is different/I am not specifying Cocoa (I don't need to) so I expect that the issues I am encountering are probably due in part to my different requirements:Compiling SDL on OS X with makefileExample:

CC=g++
CFLAGS=-c -Wall
SDLFLAGS=`sdl-config --cflags --libs` -framework Cocoa
SOURCES=main.cpp Game.cpp IO.cpp Board.cpp Pieces.cpp Piece.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=tetris

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
 $(CC) $(OBJECTS) $(SDLFLAGS) -o $@

.cpp.o:
 $(CC) $(CFLAGS) $< -o $@

clean:
 rm -rf *.o $(EXECUTABLE)

到目前为止,我的makefile文件:

My makefile so far:

CXX       = g++
CXXFLAGS  = -c -O3 -g -Wall -Wextra -std=c++1y
SDLFLAGS  = `sdl2-config --cflags --libs`
SOURCES   = hello_main.cpp hello.cpp
OBJECTS   = $(SOURCES:.cpp=.o)
EXECNAME  = hello

all: $(SOURCES) $(EXECNAME)

$(EXECUTABLE): $(OBJECTS)
    $(CXX) $ (OBJECTS) $(SDLFLAGS) -o $@

.cpp.o:
    $(CXX) $(CXXFLAGS) $< -o $@

clean :
    -rm -f *.o *.core $(EXECNAME)

在我的.hpp头文件中,如果我#include <SDL.h>并运行单线"命令,则一切都成功.如果我在上面尝试我的makefile,将无法找到,但是如果我将指令更改为#include <SDL2/SDL.h>,则会发现该库.控制台输出如下:

In my .hpp header file if I #include <SDL.h> and run the one-liner command, everything is successful. If I try my makefile above, cannot be found, but if I then change the directive into #include <SDL2/SDL.h> the library is discovered. Yet the console output is the following:

g++ -c -O3 -g -Wall -Wextra -std=c++1y    hello.cpp   -o hello

这很奇怪.

运行./hello会产生权限被拒绝"错误,该错误确认链接和编译均未成功.每个人的系统都有些不同,到目前为止,我发现的问题在这种情况下无济于事.我非常差不多可以正常工作了(但是再说一次,我将如何开始在IDE中使用它呢?我想,只要我可以导入固定的makefile或仅从终端/编辑中构建即可)从IDE,我很好.)

Running ./hello yields a "permission denied" error, which confirms that the linking and compilation were not successful.Everyone's system is a little bit different and the questions I've found so far don't help in this case.I am very close to having this working (but then again, how would I start using this in an IDE? I suppose that as long as I can import the fixed makefile or build from the terminal/edit only from the IDE, I am fine.)

我需要在makefile中进行哪些更改?谢谢.

What changes in the makefile do I need to make?Thank you.

变化1:

CXX         = g++
CXXFLAGS    = -O3 -g -Wall -Wextra -std=c++1y -c
SDLCFLAGS   = `sdl2-config --cflags`
SDLLIBFLAGS = `sdl2-config --libs`

SOURCES   = hello_main.cpp hello.cpp
OBJECTS   = $(SOURCES:.cpp=.o)
EXECNAME  = hello

all: $(SOURCES) $(EXECNAME)

$(EXECUTABLE): $(OBJECTS)
    $(CXX) $ (OBJECTS) $(SDLLIBFLAGS) -o $@

.cpp.o:
$(CXX) $(CXXFLAGS) $(SDLCFLAGS) $< -o $@

clean :
    -rm -f *.o *.core $(EXECNAME)

推荐答案

我和一个朋友聊天,发现了哪里出了问题:一堆错字和规则怪异.适用于需要基本makefile的任何人的以下方法:

I chatted with a friend and figured what was wrong: a bunch of typos and rule oddities. The following works, for anyone out there who needs a basic makefile:

CXX         = g++
CXXFLAGS    = -O3 -g -Wall -Wextra -std=c++1y
#LDFLAGS     = -lSDL2_image
SDLCFLAGS   = $(shell sdl2-config --cflags)
SDLLIBFLAGS = $(shell sdl2-config --libs)

SOURCES   = hello_main.cpp hello.cpp
OBJECTS   = $(SOURCES:.cpp=.o)
EXECNAME  = hello

all: $(EXECNAME)

$(EXECNAME): $(OBJECTS)
    $(CXX) $(OBJECTS) $(SDLLIBFLAGS) $(LDFLAGS) -o $@

%.o: %.cpp
    $(CXX) -c $(CXXFLAGS) $(SDLCFLAGS) $< -o $@

clean :
    -rm -f *.o *.core $(EXECNAME)

这篇关于通过Homebrew的SDL Max OSX,Makefile不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:51