我需要什么帮助:传递一个整数值,是在file2.c中使用file1.h通过全局变量来file3.c中如何实现一个全局变量?
你好,我使用了一个全局变量有一个问题,以确定的一个我在密文文本项目上使用的端口。它非常大,所以我刚刚添加了解决我的问题的代码片段。
我目前使用的编译内容:gcc的-o executable3 file2.c中file1.h(这工作)
和我的第二线(不工作):gcc的-o executable3 file3.c中
编译时我的错误消息:未定义的引用`全局变量” collect2:LD返回1个退出状态
我file1.h文件:
ifndef FILE1_H
define FILE1_H
include <stdio.h>
include <stdlib.h>
extern int *GLOBALVARIABLE;
endif
我的文件,我初始化全局变量值,将其设置为端口号(文件2):使编译冲突上述
#include "file1.h"
#define SIZE 128000
int *GLOBALVARIABLE;
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
portno = atoi(argv[1]);
GLOBALVARIABLE = &portno;
文件(文件3):
#include "file1.h"
#define SIZE 16000
int main(int argc, char *argv[])
{
int sockfd, portno;
portno = atoi(argv[3]);
if (portno == *GLOBALVARIABLE){
printf("Theres been a port error, you are using the wrong port for this
command");
exit(1);
请帮助! 谢谢!
您在file2和file3中有两个主要函数(),它意味着它会创建两个可执行文件?是不是你只想要一个可执行文件? – LethalProgrammer