【问题标题】:How to fix duplicated functions in object files?如何修复目标文件中的重复函数?
【发布时间】:2021-08-03 16:47:43
【问题描述】:

我正在构建一个程序只是为了提高我的 OOP 技能。这是我的项目结构:

Project
 -.vscode
 -build
 -includes
   - ContactInfo.h
   - Customer.h
   - Owner.h
   - PersonalInfo.h
   - User.h
 -src
   - ContactInfo.cpp
   - Customer.cpp
   - Owner.cpp
   - PersonalInfo.cpp
   - User.cpp
   - wrapper.h
   - wrapper.cpp
   - main.cpp
 - CMakeLists.txt

问题是当我在构建文件路径的 cmd 中键入命令 make 时,我收到以下错误:

duplicate symbol '_key' in:
    CMakeFiles/make_test_project.dir/src/main.cpp.o
    CMakeFiles/make_test_project.dir/src/wrapper.cpp.o
duplicate symbol '_customers' in:
    CMakeFiles/make_test_project.dir/src/main.cpp.o
    CMakeFiles/make_test_project.dir/src/wrapper.cpp.o
ld: 2 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [make_test_project] Error 1
make[1]: *** [CMakeFiles/make_test_project.dir/all] Error 2
make: *** [all] Error 2

我可以猜到这里发生了什么,我基本上将wrapper.h 文件包含在main.cppwrapper.cpp 文件中,但我不知道如何修复它。我也想以这种方式保持我的项目结构。那么有没有办法解决这个错误呢?

这是我的wrapper.h 文件:

#ifndef WRAPPER_H
#define WRAPPER_H

#include "../includes/Customer.h"

#include <vector>
#include <iostream>

char key;

void printUI();
void RunApplication();
PersonalInfo CreatePersonalInfo(std::string &, std::string &, int & );
ContactInfo CreateContactInfo(std::string &, std::string &, std::string &, int&);
void DisplayAllCustomers();
void AddCustomerToList(Customer &);
Customer CreateCustomer(PersonalInfo &, ContactInfo &);

std::vector<Customer> customers;

#endif

这是我的main.cpp 文件:

#include "wrapper.h"

int main()
{   
    RunApplication();
}


以下是我的CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.13)

project(cmake_test_project)

set(CMAKE_CXX_STANDARD 17)

add_executable(make_test_project src/main.cpp src/wrapper.cpp src/ContactInfo.cpp src/PersonalInfo.cpp src/Customer.cpp src/User.cpp)

注意:我不会分享 wrapper.cpp 文件,因为它是一个由近 120 行组成的文件,但我可以告诉你我还在 wrapper.cpp 文件中包含 wrapper.h 文件的信息。

请随时询问更多信息,谢谢。

【问题讨论】:

  • 请参阅c++ global object 以解决至少一个问题,但错误还提到未定义的引用,这意味着链接器无法在任何源文件中看到printUI 的定义。
  • 更改为extern char key; 并将char key; 添加到一个.cpp 文件中。
  • @Ali 然后查看链接的问题。您不能将全局变量的定义放在头文件中,除非它是 inline(自 C++17 起可行)。您应该将这些变量移动到 .cpp 文件中,并且仅将 extern 它们放在标题中,以使它们可以在其他 .cpp 文件中访问。
  • customers做同样的事情。
  • 首先不要使用全局对象。如果您确实需要它们,这只是您的约定问题,将 externs 放置在哪里。

标签: c++ oop c++11 g++ project-structure


【解决方案1】:

在你的 wrapper.h 中

extern char key;

void printUI();
void RunApplication();
PersonalInfo CreatePersonalInfo(std::string &, std::string &, int & );
ContactInfo CreateContactInfo(std::string &, std::string &, std::string &, int&);
void DisplayAllCustomers();
void AddCustomerToList(Customer &);
Customer CreateCustomer(PersonalInfo &, ContactInfo &);

extern std::vector<Customer> customers;

并在一个且只有一个 .cpp 文件中声明。

如果你有 C++17,你可以用 inline 交换 extern,你就完成了。

【讨论】:

  • 谢谢!我对customers 做了同样的事情,一切都修复了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
  • 2015-04-05
  • 2012-07-27
  • 1970-01-01
  • 2016-05-31
  • 2021-05-26
相关资源
最近更新 更多