c++ - c ++中的链接错误(多个cpp文件)
问题描述
我正在制作一个带有 3 个文件的素数生成器(其中两个是 .cpp,一个是 .h)。
但是,当我尝试在 onlinegdb 中构建整个项目时,会出现此错误
/tmp/ccI5GCGK.o: In function `main':
main.cpp:(.text+0x11f): undefined reference to `int primegen(int&, long*, long*)'
collect2: error: ld returned 1 exit status
主文件
#include <iostream>
#include "primegen.h"
int main(void)
{
//taking input for number of test cases
int test_case{2};
long int lower_lim[MAX] = {5, 15}, upper_lim[MAX] = {15, 25};
//function present in primefunc.cpp
primegen(test_case,lower_lim,upper_lim);
}
主函数.cpp
// to make SUCCESS known to this file
extern int SUCCESS;
//main function to prime generator between limits
int primegen(int &test, auto *low, auto *up)
{
static int cases=0;
if(cases == test)
return SUCCESS;
int diff=up[cases]-low[cases];
for(int i=0;i<diff;i++)
{
//some code to be added
}
}
素数
// for making arrays of lower and upper limit
constexpr int MAX = 10;
constexpr int SUCCESS = 2;
// for printing out prime number
int primegen(int &, auto *, auto *);
编辑:-我尝试将功能从第二个 cpp 移动到 main.cpp 并且它有效,并且个人构建也取得了成功。
解决方案
推荐阅读
- html - 在 html 编码语法中使用 Font Awesome
- javascript - Apps 脚本仅在特定选项卡/工作表上运行 onEdit
- reactjs - 更改不会在反应中反映在 UI 中,但是当我控制台日志时值正在更新
- symfony - 使用 Symfony 内置反向代理的 FOSHttpCacheBundle 缓存失效不起作用
- mongodb - 如何使用 mongo .net 驱动程序获取客户端上的当前连接池占用率?
- c++ - 如何替换 pow()?
- apache-flink - 使用 Apache Flink SQL 从 Kafka 消息中获取嵌套字段
- python-3.x - 如果时间戳包含一定的时间,则触发一个动作
- java - 在 jHipster 上获取“无法读取未定义的属性“替换”错误
- reactjs - ReactJS中useState Hooks的基础实现示例