Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them? Function declarations default to being extern. If you don't specify any st...
#4: Post edited
- > And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them?
- Function declarations default to being `extern`. If you don't specify any storage-class specifiers, a function prototype is `extern`.
- This is specified in C23 in 6.2.2p5:
- > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2>
- >
- > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**.
- Function definitions are only those that have a function body, so the storage-class specifier is independent.
- In the case of variables, it's different.
- > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again.
- You need not _declare_ them again. There's a difference between declarations and definitions.
With functions, it's more obvious: a function prototype declares a function, but only a function definition (those that have a function body) define a function.- With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them.
- The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers.
- The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier.
- > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value.
- > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
- > And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them?
- Function declarations default to being `extern`. If you don't specify any storage-class specifiers, a function prototype is `extern`.
- This is specified in C23 in 6.2.2p5:
- > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2>
- >
- > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**.
- Function definitions are only those that have a function body, so the storage-class specifier is independent.
- In the case of variables, it's different.
- > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again.
- You need not _declare_ them again. There's a difference between declarations and definitions.
- With functions, it's more obvious: a function prototype declares a function, but only a function definition (those that have a function body) defines a function.
- With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them.
- The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers.
- The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier.
- > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value.
- > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
#3: Post edited
- > And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them?
- Function declarations default to being `extern`. If you don't specify any storage-class specifiers, a function prototype is `extern`.
- This is specified in C23 in 6.2.2p5:
- > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2>
- >
- > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**.
- Function definitions are only those that have a function body, so the storage-class specifier is independent.
- In the case of variables, it's different.
- > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again.
- You need not _declare_ them again. There's a difference between declarations and definitions.
With functions, it's more obvious: a function prototype declares a function, but only a function declaration (those that have a function body) define a function.- With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them.
- The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers.
- The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier.
- > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value.
- > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
- > And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them?
- Function declarations default to being `extern`. If you don't specify any storage-class specifiers, a function prototype is `extern`.
- This is specified in C23 in 6.2.2p5:
- > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2>
- >
- > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**.
- Function definitions are only those that have a function body, so the storage-class specifier is independent.
- In the case of variables, it's different.
- > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again.
- You need not _declare_ them again. There's a difference between declarations and definitions.
- With functions, it's more obvious: a function prototype declares a function, but only a function definition (those that have a function body) define a function.
- With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them.
- The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers.
- The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier.
- > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value.
- > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
#2: Post edited
- > And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them?
Function declarations default to being `extern`. If you don't specify any storage class specifiers, a function prototype is `extern`.- This is specified in C23 in 6.2.2p5:
- > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2>
- >
- > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**.
- Function definitions are only those that have a function body, so the storage-class specifier is independent.
- In the case of variables, it's different.
- > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again.
- You need not _declare_ them again. There's a difference between declarations and definitions.
- With functions, it's more obvious: a function prototype declares a function, but only a function declaration (those that have a function body) define a function.
- With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them.
- The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers.
- The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier.
- > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value.
- > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
- > And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them?
- Function declarations default to being `extern`. If you don't specify any storage-class specifiers, a function prototype is `extern`.
- This is specified in C23 in 6.2.2p5:
- > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2>
- >
- > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**.
- Function definitions are only those that have a function body, so the storage-class specifier is independent.
- In the case of variables, it's different.
- > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again.
- You need not _declare_ them again. There's a difference between declarations and definitions.
- With functions, it's more obvious: a function prototype declares a function, but only a function declaration (those that have a function body) define a function.
- With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them.
- The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers.
- The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier.
- > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value.
- > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o.
- Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
#1: Initial revision
> And with the same logic, shouldn't the function prototypes in gpio.h also be extern for my source files to see them? Function declarations default to being `extern`. If you don't specify any storage class specifiers, a function prototype is `extern`. This is specified in C23 in 6.2.2p5: > <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3220.pdf#subsection.6.2.2> > > If the declaration of an identifier for a function has no storage-class specifier, its linkage is determined exactly as if it were declared with the storage-class specifier **extern**. Function definitions are only those that have a function body, so the storage-class specifier is independent. In the case of variables, it's different. > As far as I understand it, declaring variables as `extern` in pins.h makes them visible to source files that include pins.h, and I need not define them again. You need not _declare_ them again. There's a difference between declarations and definitions. With functions, it's more obvious: a function prototype declares a function, but only a function declaration (those that have a function body) define a function. With variables, it's different: automatic variables and `static` variables are not declared; they're directly defined. With `extern` variables, you can declare them without defining them. The way to define a variable with external linkage is to declare it in file scope without any storage class specifiers. The way to declare a variable with external linkage is to declare it in file scope with the `extern` storage-class specifier. > But to be honest, I'm not sure why I need extern in the first place. I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o. To disambiguate between a declaration and a definition. The definition is what assigns some storage to the name, and also the initial value. > I get a compiler error if I remove it, saying there are multiple definitions of the pins in main.o. Because by removing it, you're defining the variable in more than one translation unit, and thus assigning a different storage to the same variable name in different TUs. There must be unambiguous storage for any given variable.
