gpt4 book ai didi

c++ - Undefined reference to function错误,一起使用C和C++

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:09:51 24 4
gpt4 key购买 nike

我在同时使用 C 和 C++ 代码时遇到了问题。当从 调用时,'make' 命令为 SPConfig.cSPLogger.c 中的所有函数返回“未定义的函数引用” >SPImageProc.cpp

这些相关文件的#include部分如下:

SPLogger.c

#include "SPLogger.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

SPConfig.h

#ifndef SPCONFIG_H_
#define SPCONFIG_H_

#include <stdbool.h>
#include <stdio.h>
#include "SPLogger.h"
//Functions definitions
#endif /* SPCONFIG_H_ */

SPConfig.c

#include "SPConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

SPImageProc.h

#ifndef SPIMAGEPROC_H_
#define SPIMAGEPROC_H_
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <vector>

extern "C" {
#include "SPConfig.h"
#include "SPPoint.h"
}

namespace sp {
//Class and function definitions
}

SPImageProc.cpp

#include <cstdlib>
#include <cassert>
#include <cstring>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <cstdio>
#include "SPImageProc.h"
extern "C" {
#include "SPLogger.h"
}

生成文件

CC = gcc
CPP = g++
#put all your object files here
OBJS = main.o SPImageProc.o SPPoint.o
#The executabel filename
EXEC = SPCBIR
INCLUDEPATH=/usr/local/lib/opencv-3.1.0/include/
LIBPATH=/usr/local/lib/opencv-3.1.0/lib/
LIBS=-lopencv_xfeatures2d -lopencv_features2d \
-lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_core


CPP_COMP_FLAG = -std=c++11 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG

C_COMP_FLAG = -std=c99 -Wall -Wextra \
-Werror -pedantic-errors -DNDEBUG

.PHONY: all clean

all: $(EXEC)

$(EXEC): $(OBJS)
$(CPP) $(OBJS) -L$(LIBPATH) $(LIBS) -o $@
main.o: main.cpp #put dependencies here!
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c++ source file
#use g++ -MM SPImageProc.cpp to see dependencies
SPImageProc.o: SPImageProc.cpp SPImageProc.h SPConfig.h SPPoint.h SPLogger.h
$(CPP) $(CPP_COMP_FLAG) -I$(INCLUDEPATH) -c $*.cpp
#a rule for building a simple c source file
#use "gcc -MM SPPoint.c" to see the dependencies

SPPoint.o: SPPoint.c SPPoint.h
$(CC) $(C_COMP_FLAG) -c $*.c
clean:
rm -f $(OBJS) $(EXEC)

一些 Makefile 错误:

SPImageProc.o: In function `sp::ImageProc::initFromConfig(sp_config_t*)':
SPImageProc.cpp:(.text+0xc8): undefined reference to `spConfigGetPCADim'
SPImageProc.cpp:(.text+0xf2): undefined reference to `spLoggerPrintError'
SPImageProc.cpp:(.text+0x12c): undefined reference to `spConfigGetNumOfImages'

我已经在各自的 C 和 CPP 文件中实现了这些功能。我已经尝试了很多自己修复它并在 Stack Overflow 上查找类似问题但找不到解决方案。请帮忙。

最佳答案

您没有链接 SPLogger.oSPConfig.o

或者甚至为此编译它们。

您需要为 SPLogger.oSPConfig.o 添加类似于 SPImageProc.o 的 make 规则,您需要将它们添加到OBJS.

关于c++ - Undefined reference to function错误,一起使用C和C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43539356/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com