makefile:没有创建可执行文件

时间:2015-03-01 17:13:26

标签: c++ macos makefile g++

之前我曾使用过多的openGL和之前的SDL2。现在我尝试使用Glew,SDL2和OpenGL进行优化项目。我希望它能够在Linux(我主要使用Debian),OSX和Windows上运行。所以我试着编写自己的makefile。这段代码应该只为我创建一个窗口。 我正在开发osx。

我只有main.cpp,Display.cpp和Display.h

这是我试过的:

#my compiler
CC=g++

#my program name
PRGNAME=booom

#library search directory
LIBDIR=-L/usr/lib -L/usr/local/lib

#library names
#uses libSDL.a, libGLEW.a
LIBNAME=-lSDL -lGLEW

#includes for libraries
INCLUDES=-I/usr/include -I/usr/local/include

#my compiler options
CFLAGS=-c -Wall


all: main.o Display.o
    $(CC) $(CFLAGS) $(INCLUDES) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o

#main.cpp is the dependency of main.o
main.o: main.cpp
    $(CC) $(CFLAGS) $(INCLUDES) $(LIBDIR) $(LIBNAME) main.cpp

Display.o: Display.cpp
    $(CC) $(CFLAGS) $(INCLUDES) $(LIBDIR) $(LIBNAME) Display.cpp

clean:
    rm -rf *o booom

我得到一个main.o和一个Display.o文件,但我没有得到可执行文件。

当我做“make clean&& make”时,这是我的输出:

macbook-mehrschwein:Booom Mehrschwein$ make clean && make
rm -rf *o booom
g++ -c -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lSDL -lGLEW main.cpp
clang: warning: -lSDL: 'linker' input unused
clang: warning: -lGLEW: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/lib'
clang: warning: argument unused during compilation: '-L/usr/local/lib'
g++ -c -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lSDL -lGLEW Display.cpp
clang: warning: -lSDL: 'linker' input unused
clang: warning: -lGLEW: 'linker' input unused
clang: warning: argument unused during compilation: '-L/usr/lib'
clang: warning: argument unused during compilation: '-L/usr/local/lib'
g++ -c -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -lSDL -lGLEW -o booom main.o Display.o
clang: warning: -lSDL: 'linker' input unused
clang: warning: -lGLEW: 'linker' input unused
clang: warning: main.o: 'linker' input unused
clang: warning: Display.o: 'linker' input unused
clang: warning: argument unused during compilation: '-Wall'
clang: warning: argument unused during compilation: '-I /usr/include'
clang: warning: argument unused during compilation: '-I /usr/local/include'
clang: warning: argument unused during compilation: '-L/usr/lib'
clang: warning: argument unused during compilation: '-L/usr/local/lib'
macbook-mehrschwein:Booom Mehrschwein$ 

2 个答案:

答案 0 :(得分:0)

问题在于-c标志,您在所有阶段都使用。 它意味着编译目标文件但不链接。 删除all目标。像:

all: main.o Display.o
$(CC) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o

这解决了链接问题。 现在确保您在指定的文件夹中有libSDL。

答案 1 :(得分:0)

新版本:

all: main.o Display.o
    $(CC) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o

main.o: main.cpp
    $(CC) $(CFLAGS) $(INCLUDES) main.cpp 

Display.o: Display.cpp
    $(CC) $(CFLAGS) $(INCLUDES) Display.cpp 

不再对Display.o和main.o发出警告。

我现在得到的错误:

g++ -c -Wall -I/usr/include -I/usr/local/include main.cpp 
g++ -c -Wall -I/usr/include -I/usr/local/include Display.cpp 
g++ -Wall -I/usr/include -I/usr/local/include -L/usr/lib -L/usr/local/lib -o booom main.o Display.o -lSDL -lGLEW
ld: library not found for -lSDL
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1

编辑:名称是libSDL2.a,而不是libSDL.a

但现在它告诉我他找不到libGlew.a

libGLEW.a位于/ usr / lib

libSDL2.a位于/ usr / local / lib

他们俩都需要在同一个目录中吗?

EDIT2:

更新

我改变了一些东西,我现在将GLEW构建到" / usr / local",所以一切都在同一个目录中。

我的makefile:

#my compiler
CC=g++

#my program name
PRGNAME=booom

#library search directory
LIBDIR=-L/usr/local/lib

#library names
#uses libSDL2.a, libGLEW.a
LIBNAME=-lSDL2 -lGLEW 

#includes for libraries
INCLUDES=-I/usr/local/include

#my compiler options
CFLAGS=-c -Wall


all: main.o Display.o
    $(CC) $(LIBDIR) $(LIBNAME) -o $(PRGNAME) main.o Display.o

#main.cpp is the dependency of main.o
main.o: main.cpp
    $(CC) $(CFLAGS) $(INCLUDES) main.cpp

Display.o: Display.cpp
    $(CC) $(CFLAGS) $(INCLUDES) Display.cpp 

clean:
    rm -rf *o booom

我现在的错误:

rm -rf *o booom
g++ -c -Wall -I/usr/local/include main.cpp
g++ -c -Wall -I/usr/local/include Display.cpp 
g++ -L/usr/local/lib -lSDL2 -lGLEW  -o booom main.o Display.o
Undefined symbols for architecture x86_64:
  "_glClear", referenced from:
      Display::Clear(float, float, float, float) in Display.o
  "_glClearColor", referenced from:
      Display::Clear(float, float, float, float) in Display.o
  "_glCullFace", referenced from:
      Display::Display(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in Display.o
  "_glEnable", referenced from:
      Display::Display(int, int, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in Display.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [all] Error 1
接收就像我有一个32位版本,而我需要一个64位版本,但我自己构建,并不知道我做错了什么。通常你也会在构建之前做一些类似./config的事情。但GLEW没有配置。我在谷歌找到的唯一解决方案涉及macports,但我很乐意在没有macports的情况下做到这一点