File.cpp.o:OpenPose标志CMakeFiles /.../ main.cpp.o的多个定义:首先在这里定义

时间:2020-02-08 14:57:22

标签: c++ openpose

我已经多次发现此问题,并且在这里(在stackoverflow上)和其他地方(在Google搜索中)浏览了数十个找到的答案,但没有任何效果。最初,我将所有代码都放在一个文件中,并且一切正常。但是现在,我需要将代码拆分为多个文件,创建一些类等等,因此我创建了以下文件:

stdafx.h

#pragma once

#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>

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

// openpose
#include <openpose/flags.hpp>
#include <openpose/headers.hpp>

#include "driver-keypoints.h"

driver-keypoints.h

#pragma once

class DriverKeypoints
{
public:
    DriverKeypoints();

    static const int NOSE = 0;
    static const int NECK = 1;
    static const int R_SHOULDER = 2;
    static const int R_ELBOW = 3;
    static const int R_WRIST = 4;
    static const int L_SHOULDER = 5;
    static const int L_ELBOW = 6;
    static const int L_WRIST = 7;
    static const int MID_HIP = 8;
    static const int R_HIP = 9;
    static const int L_HIP = 12;

    int get_video_keypoints();

protected:
    void save_keypoints(const std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>>& datums_ptr,
                        std::string out_file, bool is_first);
    void configure_wrapper(op::Wrapper& op_wrapper);
};

driver-keypoints.cpp

#include "stdafx.h"

// Custom OpenPose flags
// TODO: here should not have been an absolute path
DEFINE_string(output_path, "/path/to/output/openpose/", "Path to save required keypoints.");
DEFINE_bool(no_display, false, "Enable to disable the visual display.");

void DriverKeypoints::save_keypoints(const std::shared_ptr<std::vector<std::shared_ptr<op::Datum>>> &datums_ptr,
                                     std::string out_file, bool is_first) {
}

void DriverKeypoints::configure_wrapper(op::Wrapper &op_wrapper) {
}

int DriverKeypoints::get_video_keypoints() {
}

main.cpp

#include "stdafx.h"

int main(int argc, char** argv) {
  FLAGS_disable_multi_thread = true;

  FLAGS_net_resolution = "-1x192";
  FLAGS_number_people_max = 0;

  // Parsing command line flags
  gflags::ParseCommandLineFlags(&argc, &argv, true);

  // run parse keypoints from video
  DriverKeypoints driver_keypoints;
  return driver_keypoints.get_video_keypoints();
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.14)
project(my_project)

set(CMAKE_CXX_STANDARD 14)


find_package(OpenCV REQUIRED)
find_package(CUDA REQUIRED)
find_package(Caffe REQUIRED)
find_package(gflags REQUIRED)


include_directories(
        OpenCV_INCLUDE_DIRS
        ${CUDA_INCLUDE_DIRS}
        ${CAFFE_INCLUDE_DIRS}
)

# TODO: here should not have been an absolute path
include(/home/path_to_openpose/openpose/cmake/Utils.cmake)

set(SOURCE_FILES main.cpp driver-keypoints.cpp)
add_executable(anomalies_analysis ${SOURCE_FILES})

target_link_libraries(
        anomalies_analysis
        ${OpenCV_LIBS}
        ${CUDA_LIBRARIES}
        openpose
        gflags
)

我正在关注错误

CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.data+0x0): multiple definition of `fLI::FLAGS_logging_level'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.data+0x0): first defined here
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.data+0x4): multiple definition of `fLI::FLAGS_nologging_level'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.data+0x4): first defined here
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.bss+0x0): multiple definition of `fLB::FLAGS_disable_multi_thread'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.bss+0x0): first defined here
CMakeFiles/anomalies_analysis.dir/driver-keypoints.cpp.o:(.bss+0x1): multiple definition of `fLB::FLAGS_nodisable_multi_thread'
CMakeFiles/anomalies_analysis.dir/main.cpp.o:(.bss+0x1): first defined here
...

有很多类型的答案:

  • 您应将某些内容声明为内联(但不可能)
  • 您应该只定义一次内容(但我想我正在定义,看来问题在于包含openpose/flags.hpp,但是我在#pragma once中有stdafx.h,因此这里有问题吗?)

能帮我吗?我对C / C ++编程没有足够的经验。非常感谢。

1 个答案:

答案 0 :(得分:1)

我知道这已经晚了,但我遇到了类似的问题,于是出现了这个帖子。

我的代码在一个文件中运行良好,然后当我创建一个头文件并将它#include 到多个地方时,给了我类似的错误。就我而言,我的头文件中有 DEFINE_type() 而不是 DECLARE_type(),这会导致问题,因为变量被定义了不止一次。我会仔细检查您是否在头文件中使用 DECLARE_ 方法而不是 DEFINE_ 方法。