0

我有一个项目,它使用 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;
}

这是我的生成文件:

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
4

3 回答 3

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)
于 2013-10-23T15:49:44.640 回答
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)
于 2013-10-23T15:40:10.847 回答
0

亲爱的,我遇到了同样的错误,请看这里:

升级到 Mac OS X 10.9 / Xcode 5.0.1 后出现 C++ 链接错误

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

于 2013-12-05T13:42:10.127 回答