using arm-elf-g++ to compile a simple file with a single int in it: int fred = 10; If I do arm-elf-objdump I can see fred. NOW, if I make fred a const: const int fred = 10; it is no longer visible in the object file. In fact, the object file is smaller and I think the data is not there! (I have done this with a const array and you can see its not in the object file) Note that this is NOT a startup problem, it is actually missing from the object file directly after compiling. Can anybody explain where its gone and what I should do about it? Thanks for any help. Regads, Chris.
I guess, by making it "const", the compiler can figure out you are never going to use the data (depeding on the compilation options used), so it can safely optimize them away.
OK. But although I don't use it in this file, I want to link to it from a second source file! In a separate file I have: extern const int fred; If I take the const out of both files this would work. With them in it does not. I have taken out all the compiler options to make sure they are not having any effect. Are there any options the would FORCE the output I want? Thanks for taking time to think about this. Regards, Chris.
Errm, only now I notice you're using C++. I've been thinking of C. In C++, const declares a real constant, rather than an unmodifiable object. AFAICT (I'm not a C++ expert), a const declaration must have an initializer, and the compiler will only use it when referenced within the compilation unit. IOW, you cannot have "external constants", as there is no actual object associated to it. Someone more knowledgable about C++ might correct me if I'm wrong here.
That's an interesting idea! I have just tried compiling the same under Linux. If you compile it as a 'C' file it works. If you make it a C++ file it fails! So, there are 2 questions: 1. Under Gnu-Arm even if I make it a 'C' file it still does not work. Looks like it is compiling as C++ regardless. How do I force a 'C' compile? 2. If its C++, how do I handle such a situation? Actually what I have is a bit map as a C array that I have generated. I simply want to compile it and access it from another file. Of course I want to fore it into flash memory, NOT into RAM. Thanks for some great help. Regards, Chris.
This true "const"ness in C++ only holds for some scalar datatypes, not arrays.
Chris Williams wrote: > 1. Under Gnu-Arm even if I make it a 'C' file it still does not work. > Looks like it is compiling as C++ regardless. How do I force a 'C' > compile? Is your source file named with .cpp, .cc or .C (capital C) extension? Especially the capital C case can be a bugger on Windows since Windows filesystems are case insensitive, however GCC treats the filename case sensitively AFAIK. If so, gcc will compile it as C++ even if called via "gcc". You can force it to use C via the "-x c" parameter. The parameter has to come before the first source file on the command line, since it only affects language selection for files following it. > 2. If its C++, how do I handle such a situation? Actually what I have is > a bit map as a C array that I have generated. Define it as "extern const int fred = 10;", this will put it into the object file (as constant data, just like for example string literals) even when compiling as C++. Andreas
Yes. That does the trick. I have not used C++ in the embedded environment before so I have a bit to learn! Thanks to all who replied. Regards, Chris.
> In C++, const declares a real constant, rather than an unmodifiable > object. It's both. You can use it as a real constant, e.g. for array sizes or template arguments, but you can still e.g. take its address. > AFAICT (I'm not a C++ expert), a const declaration definition > must have an initializer, Yes, unless it's of a class type with a default constructor. > and the compiler will only use it when referenced within > the compilation unit. Even with const, it's still a regular variable. The difference is that has internal linkage by default, so it's as if you had made it static.
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
Log in with Google account
No account? Register here.