我的 dll 中带有类的未解析外部符号

问题描述 投票:0回答:0

我正在尝试使用 OpenCV 和 CMake 制作一个检测应用程序。我用两个派生类创建了一个抽象检测器类。当我在 main.cpp 中使用任何与类相关的东西时,无论是构造函数还是常规方法,我都会收到该方法的链接器错误。

解决方案有一个可执行文件和两个 dll。有问题的类在其中一个 dll 中。

未解析的外部符号“public: __cdecl FaceDetector::FaceDetector(struct detectorProperties)” (??0FaceDetector@@QEAA@UdetectorProperties@@@Z) 在函数 main 中引用

ObjectDetection.h

#pragma once
#include <opencv2/opencv.hpp>

#ifdef OBJECTDETECTION_EXPORTS
#define OBJECTDETECTION_API __declspec(dllexport)
#else
#define OBJECTDETECTION_API __declspec(dllimport)
#endif

extern OBJECTDETECTION_API void drawLabel(cv::Mat & image, std::string label, int left, int top);

extern OBJECTDETECTION_API struct detectorProperties {
    // some variables
};

extern OBJECTDETECTION_API class Detector {
protected:
    std::string modelPath;
    bool shouldSwapRB = false;
    bool active = false;
    bool shouldDrawRect = true;

public:
    virtual void detect(cv::Mat& image) = 0;
};

extern OBJECTDETECTION_API class FaceDetector : public Detector {
private:
    cv::CascadeClassifier cs;
    std::vector<cv::Rect> facesInFrame;
public:
    FaceDetector(detectorProperties props);
    void detect(cv::Mat& image);

};

ObjectDetection.cpp

#include "ObjectDetection.h"

#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
using namespace std;

void drawLabel(cv::Mat & image, string label, int left, int top) {
    // function code
}

FaceDetector::FaceDetector(detectorProperties props) {
    // constructor code
}

// code

main.cpp

#include "ObjectDetection.h"

int main() {
    detectorProperties props;
    props.modelPath = "some/path";

    FaceDetector det = FaceDetector(props);

我可以创建结构的实例并调用

drawLabel
,但是类不起作用。

CM做输出

Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22623.
The C compiler identification is MSVC 19.35.32216.1
The CXX compiler identification is MSVC 19.35.32216.1
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working C compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
Detecting C compile features
Detecting C compile features - done
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Check for working CXX compiler: C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe - skipped
Detecting CXX compile features
Detecting CXX compile features - done
OpenCV ARCH: x64
OpenCV RUNTIME: vc16
OpenCV STATIC: OFF
Found OpenCV 4.7.0 in C:/OpenCV/build/x64/vc16/lib
You might need to add C:\OpenCV\build\x64\vc16\bin to your PATH to be able to run your applications.
Could NOT find WrapVulkanHeaders (missing: Vulkan_INCLUDE_DIR) 
Configuring done (4.5s)
Generating done (0.2s)

顶级 CMake 文件

    # Qt 6.2.4 uses CMake 3.16, so that should be our minimum CMake version
cmake_minimum_required(VERSION 3.16)

# Set the name for the project
project(DetectionApp)

# Specify where should the executables and libraries created
# This helps us avoid copying the files manually or using a post-build command
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMALE_PREFIX_PATH $ENV{Qt6_DIR})

set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_AUTOUIC_SEARCH_PATHS} src/Application)

find_package(OpenCV REQUIRED)
find_package(Qt6 REQUIRED COMPONENTS Gui Core Widgets)

# Add the subdirectories for the different parts of the application
add_subdirectory(src/Application)
add_subdirectory(src/CameraInteraction)
add_subdirectory(src/ObjectDetection)

add_subdirectory(src/CameraInteraction_v2)
add_subdirectory(src/ObjectDetection_v2)
add_subdirectory(src/WebCam)

set(CMAKE_VERBOSE_MAKEFILE ON)

应用CMake文件

# Set the project name
project(Application)

# Add the executable
file(GLOB SOURCE_FILES "*.cpp")
file(GLOB UI_FILES "*.ui")
file(GLOB HEADER_FILES "*.h")
add_executable(${PROJECT_NAME} ${HEADER_FILES} ${SOURCE_FILES} ${UI_FILES})

# Add the libraries
add_dependencies(${PROJECT_NAME}
    CameraInteraction
    ObjectDetection
)

# Allow the headers to be included in main.cpp
target_include_directories(${PROJECT_NAME} PUBLIC
                          "${CMAKE_SOURCE_DIR}/src/CameraInteraction"
                          "${CMAKE_SOURCE_DIR}/src/ObjectDetection"
                           ${OpenCV_INCLUDE_DIRS}
                          )
                    
#Link this project with te runtime output dir
target_link_directories(${PROJECT_NAME} PUBLIC
    ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)

target_link_libraries(${PROJECT_NAME} 
    CameraInteraction
    ObjectDetection
    Qt6::Core
    Qt6::Widgets
    ${OpenCV_LIBS}
)

set_target_properties(${PROJECT_NAME} PROPERTIES
    WIN32_EXECUTABLE ON
    MACOSX_BUNDLE ON
)

库 CMake 文件

# Set the project name
project(ObjectDetection)

# Take every header and source in the directory
file(GLOB HEADER_FILES "*.h")
file(GLOB SOURCE_FILES "*.cpp")

# Add the library
add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES})

# Link against OpenCV
target_include_directories(${PROJECT_NAME} PUBLIC ${OpenCV_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} PUBLIC ${OpenCV_LIBS})

如果我创建一个简单的类,例如:

class Test {
public:
  Test() {}
}

我还是不能在main里初始化它

c++ class linker abstract-class linker-errors
© www.soinside.com 2019 - 2024. All rights reserved.