Makefile在mac os x Mavericks上不起作用

时间:2013-10-23 15:10:27

标签: c++ macos opencv makefile

我有一个使用opencv库的项目。今天我已经将我的Mac OS X Lion升级为Mavericks,现在我想知道为什么它不再编译因为以下错误:

c++ -O2 -g -Wall -fmessage-length=0    -c -o Hello.o Hello.cpp
Hello.cpp:2:10: fatal error: 'opencv/cv.h' file not found
#include "opencv/cv.h"
         ^
1 error generated.
make: *** [Hello.o] Error 1

这是Hello.cpp的代码:

#include <iostream>
#include "opencv/cv.h"

using namespace std;

int main(){
    return 0;
}

这是我的makefile:

CXXFLAGS =  -O2 -g -Wall -fmessage-length=0 

OBJS =      Hello.o

LIBS =  -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_gpu


INCPATH = -I/usr/local/Cellar/opencv/2.4.6.1/include
LIBPATH = -L/usr/local/Cellar/opencv/2.4.6.1/lib

TARGET =    Hello

$(TARGET):  $(OBJS)
    g++ $(INCPATH) $(LIBPATH) -o $(TARGET) $(OBJS) $(LIBS) 


all:    $(TARGET)


clean:
    rm -f $(OBJS) $(TARGET)

我必须说我可以使用终端编译我的项目:

g++ `pkg-config --cflags opencv` Hello.cpp -o Hello

并且确定,这是我在终端上运行pkg-config --cflags opencv所得到的:

-I/usr/local/Cellar/opencv/2.4.6.1/include/opencv

3 个答案:

答案 0 :(得分:3)

我怀疑这个makefile是否有效,除非新版本的MacOSX将这些标题从标准位置移到另一个位置。

您正在使用默认的内置规则,正如Paul R所说,但您没有使用符合这些规则的默认make变量。我会写这样的makefile:

CXX = g++
CXXFLAGS = -O2 -g -Wall -fmessage-length=0 
CPPFLAGS = -I/usr/local/Cellar/opencv/2.4.6.1/include

OBJS =      Hello.o

LDFLAGS = -L/usr/local/Cellar/opencv/2.4.6.1/lib
LDLIBS =  -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video \
          -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect \
          -lopencv_contrib -lopencv_legacy -lopencv_gpu

TARGET =    Hello

.PHONY: all
all: $(TARGET)
$(TARGET):  $(OBJS)
         $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

.PHONY: clean
clean:
        rm -f $(OBJS) $(TARGET)

答案 1 :(得分:1)

makefile没有用于构建Hello.o的明确规则,因此它使用默认规则。有多种方法可以解决这个问题,但我只想将其更改为:

CXXFLAGS =  -O2 -g -Wall -fmessage-length=0 

SRCS =      Hello.cpp

LIBS =  -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_gpu

INCPATH = -I/usr/local/Cellar/opencv/2.4.6.1/include
LIBPATH = -L/usr/local/Cellar/opencv/2.4.6.1/lib

TARGET =    Hello

$(TARGET):  $(SRCS)
    g++ $(CXXFLAGS) $(INCPATH) $(LIBPATH) -o $(TARGET) $(SRCS) $(LIBS) 

all:    $(TARGET)

clean:
    rm -f $(TARGET)

答案 2 :(得分:0)

亲爱的我得到了同样的错误,请看一下:

C++ linking error after upgrading to Mac OS X 10.9 / Xcode 5.0.1

非常快速的解决方案:将-stdlib = libstdc ++添加到链接命令。