简体   繁体   English

如何在另一个C文件中访问指向在一个C文件中声明的字符串文字的外部指针?

[英]How can an extern pointer to string literal declared in one C file be accessed in another C file?

I'm getting my sea legs with C, and I need a string literal which is discovered in a main function in one .c file to be accessible in an entirely different .c file. 我正在使用C语言,而且我需要一个字符串文字,它可以在一个.c文件的main函数中发现,以便可以在一个完全不同的.c文件中进行访问。

What I've tried: I'm declaring a pointer to a string literal as extern in a header, defining it on one .c file, and trying to access the literal in a second .c file. 我已经尝试的方法:我在标头中将指向字符串文字的指针声明为extern ,在一个.c文件中定义了它,并尝试在第二个.c文件中访问文字。 Compilation is failing due to lack of definition in the second (accessing) file. 由于第二个(正在访问)文件中的定义不足,编译失败。

What I've got in a nutshell: 简而言之:

file1.h : file1.h

extern char *global;

file1.c : file1.c

#include "file1.h"

int main() {
     //some code
     extern char *global = some_struct->data;
     //more code
}

file2.c : file2.c

#include "file1.h"

int do_stuff() {
     //some code
     some_function(global);
     //more code
}

I expected this to compile, since global is declared in file1.h , defined in file1.c , and its value accessed in file2.c . 我希望可以编译该文件,因为globalfile1.h声明,在file1.c定义,并且其值在file2.c访问。 Compilation of file2.c fails, however, for undefined variable. 但是,对于未定义的变量, file2.c编译失败。

If relevant, the main function in file1.c is always the first function run in the program. 如果相关, file1.cmain函数始终是程序中运行的第一个函数。

in main 主要

  extern char *global = some_struct->data; 

is wrong, if you want to assign global do : 是错误的,如果要分配全局 do:

     global = some_struct->data;

and some where at global scope you need to define global : 还有一些需要在全局范围内定义global的地方

char *global;

So for instance main become : 因此例如main变为:

#include "file1.h"

char *global;

int main() {
     //some code
     global = some_struct->data;
     //more code
}

You're close, but not quite there. 您已经接近,但还没到那儿。 The global variable must also be defined in the global scope, and you're running into the difference between declaration and definition (which are easy to get the wrong way round but the words don't matter as much as the idea). 全局变量也必须在全局范围内定义 ,并且您会遇到声明定义之间的差异(很容易以错误的方式绕过,但这些单词与想法无关紧要)。 Normally, your statements that introduce a variable will both declare and define simultaneously, but not so with globals that are shared between modules like this: 通常,引入变量的语句将同时声明和定义,但对于在模块之间共享的全局变量则不是这样:

In your file1.h , you are correctly declaring the char* variable in the global scope. file1.h ,您正确地在全局范围内声明char*变量。 Anyone who includes this file (eg file2.c ) will "see" that the variable with that name should exist and all modules will compile cleanly, and the extern keyword makes explicit that you are only declaring this to exist, not creating storage for it yet. 包含此文件的任何人(例如file2.c )都会“看到”具有该名称的变量,并且所有模块都将进行干净地编译,并且extern关键字明确表明您仅声明它存在,而不为其创建存储。然而。 That is what you want, because you don't want to accidentally create multiple conflicting global variables with this name. 那就是您想要的,因为您不想意外地使用此名称创建多个冲突的全局变量。 When the compiled modules are eventually linked together, the linker will look for the actual memory storage space set aside for the global and connect all the references up correctly. 当最终将编译的模块链接在一起时,链接器将查找为全局预留的实际内存存储空间,并正确连接所有引用。

But in your case, that won't happen correctly, because although you have declared to the world that this global exists, you haven't yet actually created storage for the variable! 但是在您的情况下,这将不会正确发生,因为尽管您已经向世界宣布此全局存在,但您尚未实际为变量创建存储!

You still need to do this in the global scope (outside the functions) in file1.h : 您仍然需要在file1.h的全局范围(功能之外)中file1.h

char * global; 

That creates the actual variable with the matching name to the extern declaration, and so the linker will be able to assemble your modules correctly. 这将创建具有与extern声明匹配的名称的实际变量,因此链接器将能够正确地组装模块。

So 所以

#include "file1.h"

// Define global:
char * global;

int main() {
     // Assign a value to the global variable:
     global = some_struct->data;

     //more code
}

Note that you don't redeclare the extern or anything in this file, and you don't redefine the type of global inside main , because it already exists, and you can assign to it or look at its value. 请注意,您不会在该文件中重新声明extern或其他任何内容,也不会重新定义main内部的global的类型,因为它已经存在,您可以为其分配值或查看其值。 You also don't use the keyword extern here-- it is the "home" module for this variable. 您在这里也不要使用关键字extern它是此变量的“ home”模块。

Declaration and definition are generally just accomplished simultaneously, for example when you declare local variables within a function. 声明和定义通常是同时完成的,例如,在函数中声明局部变量时。 Globals that are accessible from different modules rely on splitting the work into the two separate ideas so that both coworkers and the compiler and linker toolchain are not confused by your intentions. 可从不同模块访问的全局变量依赖于将工作分成两个单独的想法,以使同事,编译器和链接器工具链都不会因您的意图而混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM