c++ - 在 G++ 中编译时出现 C++ 多定义错误
问题描述
我在编译时遇到了一些问题,我不知道为什么。我得到了一些东西的多重定义错误,但是,据我所知,我只定义了一次。我对为什么会发生这种情况以及这些定义发生在哪里以及为什么它不起作用感到困惑。我对 C++ 不太熟悉,所以我不确定我在这里做错了什么,希望能得到一些帮助。
制作
/usr/bin/g++ -c -o object/author.o src/author.cpp -g -I./include
/usr/bin/g++ -o ---- object/connection_manager.o object/author.o. object/control_header_lib.o object/network_util.o object/main.o object/control_handler.o -g -I./include
object/main.o:(.bss+0x4): multiple definition of `control_socket'
object/connection_manager.o:(.bss+0x104): first defined here
object/main.o: In function `main':
/home/Documents/---/src/main.cpp:9: multiple definition of `router_socket'
object/connection_manager.o:/home/Documents/---/src. /connection_manager.cpp:35: first defined here
object/main.o: In function `main':
/home/Documents/---/src/main.cpp:9: multiple definition of `data_socket'
object/connection_manager.o:/home/Documents/---/src. /connection_manager.cpp:35: first defined here
object/control_handler.o:(.bss+0x0): multiple definition of `CONTROL_PORT'
object/main.o:/home/Documents/---/src/main.cpp:9: first defined here
object/connection_manager.o: In function `main_loop()':
/home/Documents/---/src/connection_manager.cpp:33: undefined reference to `new_control_conn(int)'
/home/Documents/---/src/connection_manager.cpp:54: undefined reference to `isControl(int)'
/home/Documents/---/src/connection_manager.cpp:56: undefined reference to `control_recv_hook(int)'
collect2: error: ld returned 1 exit status
Makefile:19: recipe for target '----' failed
make: *** [----] Error 1
主文件
#include "../include/global.h"
#include "../include/connection_manager.h"
using namespace std;
int sockfd;
int main(int argc, char **argv)
{
if(argc != 2){
ERROR("Incorrect number of args...\n");
}
printf("control port: %d\n", atoi(argv[1]));
sscanf(argv[1], "%" SCNu16, &CONTROL_PORT);
init();
return 0;
}
全局.h
#ifndef GLOBAL_H_
#define GLOBAL_H_
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#define ERROR(err_msg) {perror(err_msg); exit(EXIT_FAILURE);}
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
#endif
连接管理器.cpp
#include <sys/select.h>
#include "../include/connection_manager.h"
#include "../include/global.h"
#include "../include/control_handler.h"
int control_socket, router_socket, data_socket;
void init()
{
control_socket = create_control_sock();
}
连接管理器.h
#ifndef CONNECTION_MANAGER_H_
#define CONNECTION_MANAGER_H_
void init();
#endif
control_handler.cpp
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <sys/queue.h>
#include <unistd.h>
#include <string.h>
#include <list>
#include "../include/global.h"
#include "../include/network_util.h"
#include "../include/control_header_lib.h"
#include "../include/author.h"
#ifndef PACKET_USING_STRUCT
#define CNTRL_CONTROL_CODE_OFFSET 0x04
#define CNTRL_PAYLOAD_LEN_OFFSET 0x06
#endif
struct ControlConn // linked list for active control connections
{
int sockfd;
// ..
}*connection, *conn_temp;
std::list<ControlConn> control_conn_list;
int create_control_sock()
{
int sock;
struct sockaddr_in control_addr;
socklen_t addrlen = sizeof(control_addr);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
ERROR("Failed to create socket...\n");
}
bzero(&control_addr, addrlen);
control_addr.sin_family = AF_INET;
control_addr.sin_addr.s_addr = htonl(INADDR_ANY);
control_addr.sin_port = htons(CONTROL_PORT);
if (bind(sock, (struct sockaddr *)&control_addr, addrlen) < 0){
ERROR("Bind failed...");
}
if (listen(sock, 5) < 0){
ERROR("Listen failed...");
}
return sock;
}
control_handler.h
#ifndef CONTROL_HANDLER_H_
#define CONTROL_HANDLER_H_
int create_control_sock();
int new_control_conn(int sock_index);
bool isControl(int sock_index);
bool control_recv_hook(int sock_index);
#endif
解决方案
在头文件中,您只需要声明变量:
extern uint16_t CONTROL_PORT;
然后在相应的 cpp ( global.cpp
) 文件中定义它:
uint16_t CONTROL_PORT;
推荐阅读
- javascript - 引导模式联系表单提交不起作用
- python - 制作 Django Rest Framework (DRF) 工作流的方法
- vue.js - 在 Vue 路由解析之前访问 Vuex
- r - 手动将图例添加到 ggplot2
- networking - 如何使用 neo4j algo.pageRank 计算子图的 pagerank
- python - RuntimeError:cuda 运行时错误(30):torch/csrc/cuda/Module.cpp:32 的未知错误
- java - 在表中插入主键的问题
- java - Hibernate.initialize(...) 的问题我得到了 LazyInitializationException 但是当我调试时一切都很好
- swift - 选择单元格时出现奇怪的缩进和 imageView 行为
- node.js - jhipster app:打包时出现gulp错误