我遇到了一些编译问题,我不知道为什么。我得到了一些多重定义错误,但是,据我所知,我只定义了一次。我很困惑为什么会发生这种情况以及这些定义发生的地方以及为什么它不起作用。我对C ++并不熟悉,所以我不确定我在这里做错了什么,并希望得到一些帮助。
请
ConcurrentQueue
的main.cpp
/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
global.h
#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;
}
connection_manager.cpp
#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
connection_manager.h
#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();
}
control_handler.cpp
#ifndef CONNECTION_MANAGER_H_
#define CONNECTION_MANAGER_H_
void init();
#endif
control_handler.h
#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;
}
答案 0 :(得分:1)
在头文件中,您只需要声明变量:
extern uint16_t CONTROL_PORT;
然后在相应的cpp(global.cpp
)文件中定义它:
uint16_t CONTROL_PORT;