- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
首先让我说我不是最好的 C++,而且我对 Linux 知之甚少。对于一个类项目,我们必须实现一个堆,所以我在我的 Windows PC 上编写了所有代码,假设我可以将文件上传到学校拥有的 Linux 存储库。 [也许这是我出错的地方,这不能简单地完成。] 我的代码编译并清除了我的 Windows 电脑上提供的所有测试用例。当我将文件上传到 Linux 时,我创建了一个 makefile,当我使用 make 命令时,我得到了一个包含多个定义错误的 list 。我使用的每个函数一个错误。我做了一些搜索,但比开始时更加困惑。
我的文件是:main.cpp、main.h、heap.cpp、heap.h、util.cpp 和 util.h。
我认为问题出在我的 include 语句上,但我不是 100% 确定。
这是文件的示例。
主要.cpp
#include <iostream> //needed to use basic inputs/outputs
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "util.cpp"
#include "heap.cpp"
#include <fstream>
using namespace std;
main.h 是空白的。
堆.cpp
#include <iostream> //needed to use basic inputs/outputs
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
#include <cmath>
#include <math.h>
using namespace std;
//expanded functions found in the heap.h file
堆.h
//2 structs
//9 functions
工具.cpp
#include <iostream> //needed to use basic inputs/outputs
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
using namespace std;
//expanded functions found in util.h
工具.h
//1 function
在 heap.h 和 util.h 之间,我有 10 个函数,在运行 make 命令时,我收到关于所有 10 个的警告:
multiple definition of 'FUNCTIONNAME'
main.o:main.cpp:(.text+0x1b7): first defined here
我假设 0x1b7 是一个内存位置,因为它们各不相同。
如有任何帮助,我们将不胜感激。
最佳答案
您没有显示 Makefile,但很可能,它包含此规则或类似规则
program: main.o heap.o util.o
$(CXX) $(CXXFLAGS) -o program main.o heap.o util.o
现在发生了什么,编译器构建了三个目标文件 main.o、heap.o 和 util.o。接下来将目标文件链接在一起以构建程序
。
链接器会看到分别在 main.o 和 heap.o 或 main.o 和 util.o 中定义的各种函数的定义。这就是为什么它提示“‘FUNCTIONNAME’的多重定义”
为什么这些函数定义了不止一次?
当您将文件包含到另一个源中时,就好像您复制了 #include
位置的内容。这意味着在 heap.cpp 中定义了一个函数:
void create_heap(int size)
{
// create a heap ...
}
被逐字复制到 main.cpp 中,其中行
#include "heap.cpp"
是。
因为 heap.cpp 有 create_heap()
的定义,而 main.cpp #include
是 heap.cpp 的内容,两者都包含自己的 创建堆()
。现在编译 heap.o 和 main.o,并将它们链接在一起。每个目标文件都有一个 create_heap()
的拷贝,这就是链接器感到困惑和提示的地方
multiple definition of 'create_heap'
main.o:main.cpp:(.text+0x1b7): first defined here
要解决此问题,只需替换包含 cpp 源的行,例如
#include "util.cpp"
#include "heap.cpp"
各自的头文件
#include "util.h"
#include "heap.h"
只保留与main.cpp相关的函数定义,除此之外别无其他。现在 main.cpp 没有属于 util.cpp 或 heap.cpp 的函数定义,链接器错误也消失了。
据推测,这在 Windows 上有效,因为只有 main.cpp 包含在项目文件中,因此在生成的可执行文件中只有一个定义(来自 main.o)。
如果您在 Linux Makefile 中包含了所有源代码,那么该错误也可能在 Windows 中出现。
关于C++ 在 Windows 中编译,在 Linux 中出现多重定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49352507/
在complier.h中有一个宏定义如下: # define __cond_lock(x,c) ((c) ? ({ __acquire(x); 1; }) : 0) 但是这里我有一个问题,就是哪里
curl_easy_setopt 的选项在哪里?定义?我试图寻找 CURLOPT_VERBOSE 和其他一些整数值,但这些似乎没有在 curl.h 中明确定义。 最佳答案 第 792 行: #ifde
我确实有一个如下所示的类(class): //.h file class __declspec(dllimport) MyClass { public: //stuff pri
作者: zhuwenzhuang, 2024.05.08. 阅读前假设读者熟悉数据库使用,了解 SQL 的语法和关系算子的大概含义, 能通过 EXPLAIN 命令查看数据库执行计划. 0 前言
我似乎无法找到是否可以声明一个 header 对象以便在响应 header 中重用它,有一些示例定义了响应模式的对象,但它不会转置为响应 header 。我只设法制作了一个可重用的响应对象,如下所示:
css 选择器 * + * 实际上是什么意思?当您执行检查元素时,您可以在谷歌浏览器的控制台中看到它。在我看来,这似乎是对 "Every second child"应用一种风格,但仍然想确定。谁能帮我
我试图弄清楚基本的IO Haskell 函数是定义好的,所以我使用了this reference我到了putChar函数定义: putChar :: Char -> IO () putChar
我得到了一个自动生成的文件,该文件定义了程序集属性,我正在尝试理解内容。 [assembly: global::System.Runtime.Versioning.TargetFrameworkAtt
This文档演示了如何检查变量是否先前已在 gnuplot 脚本中定义。 文档中的示例: a = 10 if (exists("a")) print "a is defined" if (!exist
好吧,这是一个相当基本的问题:我正在关注 SICP 视频,我对 define、let 和 之间的区别有点困惑设置!. 1) 根据 Sussman 在视频中的说法,define 只允许为变量附加一个值一
我一直在尝试定义一个包含只能具有以下三个值之一的字段的 XSD: 绿色 红色 蓝色 本质上,我想在架构级别定义严格的枚举。 我的第一次尝试似乎是错误的,我不确定修复它的“正确”方法。
有人可以定义“POCO”到底是什么意思吗?我越来越频繁地遇到这个术语,我想知道它是否仅与普通类有关还是意味着更多? 最佳答案 “普通旧式 C# 对象” 只是一个普通的类,没有描述基础结构问题或域对象不
在我经常看到的一些django模型中 myfield = models.CharField(_('myfield')) class_name = models.CharField(_('Type'),
每当 BOOL 数据类型不容易预定义时,我都会使用以下定义进行 boolean 运算, typedef unsigned char BOOL; (由于内存使用)。 我意识到出于性能原因,使用本地总线宽
l_ABC_BEANVector = utilRemote.fnGetVector("ABC_COVBEANVector"); 编码的含义是什么?任何帮助,我真的很感激。谢谢 最佳答案 唯一可以肯定地
我正在使用 javacc 开发一个项目,我遇到问题并需要一些帮助,我的文件中有这样的内容: STRING COPYRIGHT (C) 2003, 2004 SYNOPSYS, INC.; 我为单词 S
我想弄清楚基本的 IO定义了 Haskell 函数,所以我使用了 this reference然后我到了 putChar函数定义: putChar :: Char -> IO () putCha
我在具体类中使用 @property 定义 getter 时遇到问题。这是Python代码: from abc import ABCMeta, abstractproperty class abstr
我正在为大学用 C 语言编写一个小游戏,但我陷入了困境。我(在头文件中)有这个结构: typedef struct{ game_element field[MAX_ROWS][MAX_COLU
我一直在 .l 文件中创建标记定义。由于数据集数量庞大,它变得有点乏味。有没有办法读取文件中的所有单词,例如包含所有名词的 noun.txt 并给所有名词一个标记。 基本上,我想自动化这部分: %%
我是一名优秀的程序员,十分优秀!