Thread: How to use extern...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    37

    How to use extern...

    Hello. I would like help in a problem I have using "extern".

    I had a cpp file (say Font.cpp) containing a constant array like

    Code:
    const uint8_t SmallFont[1144] ={   ....}
    and I wanted to use this in a main.cpp file

    Code:
    extern uint8_t SmallFont[]; 
    
    someFunction(SmallFont);
    However I got an error saying "Undefined symbol"

    My question is how to correctly use extern to be able to use the array...


    ---------------------------------------------------------------------------
    I seldom use extern. I solved the problem above by programming the way I always do:
    Copy paste all the data to a h file: Font.h and then including that file in main.cpp
    Code:
    #include "Font.h"
    that solves everything but I am wishing to learn the other way (using extern)

    thanks

  2. #2

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    Yes, I already read that and it was very confusing. First they were using h files. Why h files, if I am using extern I shouldnt need the h files, right?

    A part of that link that I found informative was the reply by Il Divin Codino in the end where he says:

    In C++, a const variable has internal linkage by default (not like C).
    Is this true? Perhaps that was my mistake??
    Because I was using a const, therefore it was considered internal and therefore even though I used extern it remained an "undefined symbol"???

    I am going to try to use then in Font.cpp
    Code:
    extern constuint8_t SmallFont[1144] ={   ....}
    What do you guys think??

  4. #4
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    It doesn't make any sense to have an extern const.

    I assume your font is built in, so declare it in the source file that you manage it in. And if you need to use it elsewhere, pass it around by pointer.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    37
    Quote Originally Posted by Yarin View Post
    It doesn't make any sense to have an extern const.
    Why?

    I assume your font is built in, so declare it in the source file that you manage it in. And if you need to use it elsewhere, pass it around by pointer.
    The thing is that for reasons of clarity ,order I want to keep main.cpp as small as possible. Also I am going to use this in main.cpp but declaring it there does not make sense since I want to write this Font once and not rewriting it everytime I want to use it.
    And the font has to be const because ...well because it doesnt change but also because in that way it goes to Flash and not SRAM (developing for a small device)

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by KansaiRobot View Post
    Why?
    Correction: It's not a good idea. Global variables should be contained as much as possible.


    Quote Originally Posted by KansaiRobot View Post
    The thing is that for reasons of clarity ,order I want to keep main.cpp as small as possible. Also I am going to use this in main.cpp but declaring it there does not make sense since I want to write this Font once and not rewriting it everytime I want to use it.
    Is it clear to try to manage a variable in a unit that it wasn't declared in? It sounds like you need to refactor your code. If you don't want to declare the font in main.cpp, then move your management code to font.cpp, and use it through a pointer or descriptor in main.cpp.


    Quote Originally Posted by KansaiRobot View Post
    And the font has to be const because ...well because it doesnt change but also because in that way it goes to Flash and not SRAM (developing for a small device)
    Of course, I wasn't suggesting the font wouldn't be const.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by KansaiRobot
    A part of that link that I found informative was the reply by Il Divin Codino in the end where he says:
    In C++, a const variable has internal linkage by default (not like C).
    Is this true? Perhaps that was my mistake??
    Yes, that is true:
    Quote Originally Posted by C++11 Clause 3.5 Paragraph 3
    A name having namespace scope has internal linkage if it is the name of
    — a variable, function or function template that is explicitly declared static; or,
    — a variable that is explicitly declared const or constexpr and neither explicitly declared extern nor previously declared to have external linkage; or
    — a data member of an anonymous union.
    Quote Originally Posted by KansaiRobot
    Because I was using a const, therefore it was considered internal and therefore even though I used extern it remained an "undefined symbol"???
    You used extern in the forward declaration, but not in the definition.

    Quote Originally Posted by KansaiRobot
    I am going to try to use then in Font.cpp
    Code:
    extern constuint8_t SmallFont[1144] ={   ....}
    What do you guys think??
    Yes, though of course you need to get the spelling correct

    Quote Originally Posted by Yarin
    It doesn't make any sense to have an extern const.
    (...)
    Correction: It's not a good idea. Global variables should be contained as much as possible.
    This is a global constant, not the usual global variable that introduces global state, so it is fine as long as it is suitably named, especially when declared in an appropriate namespace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern var
    By siperi in forum C Programming
    Replies: 2
    Last Post: 12-09-2010, 09:47 AM
  2. Replies: 17
    Last Post: 12-15-2006, 11:02 AM
  3. using extern, please help
    By Stevo in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2004, 06:18 PM
  4. What is extern and how is it used?
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-06-2001, 07:00 AM