I'm writing code using Qt under Linux (Ubuntu) for an embedded device.
I arranged a header file with global variables declaration inside; when I tested my project under Linux on my PC, everything is OK. When I try my project with the embedded system, sometimes it doesn't work, because I forgot to initialize some variables (seems that on my PC, Qt automatically set to "0" their values, while on the embedded they can have a random value).
Is there a way to initialize in one step all my variables, in such a way that when I pass them to my embedded the global variables are already correctly initialized?
Can you show us some code ?
When using global variables, you need to define them in a C/C++ file and initialize them here. The declaration in the header only says "Hey ! this one exists ! " even if not...
What I wanna do is to be sure that every global variable I create in my header file is initialized with "0" value. Is there a method, a pragma, an additional C++ header to do this?
Related
I have a pretty large application which holds most of the program data in a large container object called system.
So I access things all the time, e.g. printf("%s\n",system.constants.jobname);, and changing the name of system is undesirable.
I learned later that the function system exists as a native C function for running terminal commands, e.g. system("rm *.txt");
My problem is that I get compilation errors trying to use system as a function because it's already defined as an object.
Is there any way one can call a native C function explicitly ignoring programmatically defined variables? Or give the native system function an alias? (I'm using C++ so using it would be fine)
If you're using C++, system is in the global namespace. Assuming you've put your stuff in a proper namespace (you have, right?) you can refer to it as ::system.
Assuming using shared libraries is an acceptable solution, you can do this.
Create another C file which will not use your system container. Now write a function my_system that is a wrapper to system.
By wrapper I mean, it takes the same argument and calls system and returns what system returns.
Don't forget to export my_system
Now compile this as a dll (or .so on *NIX).
In your main project, load the dll and get a handle. Now query for address of my_system on the handle and make the call using function pointer.
As observed in the screenshot above, I have the problem that variables in the Locals and Expressions window in QT Creator are displayed as being . How can I get Qt Creator to show the values for the OpenCV filter functions?
QtCreator marks "not accessible" for the value, not for the variable. That means those variables are not initialized yet. Try to inspect the same values by adding another debugging point after the matrices are initialized.
I was getting this for a struct I had defined. I could see the contents of other structs, but not this particular one. It turned out that I was declaring the struct inside a function in my main window object. I just had to take the struct out of the function, just declaring it at global level before the function (not even bothering to put it in the header, although that's better practice). That was enough to allow me to see the values of a QList, that is itself still declared in the function in question.
I broke my Visual Studio project (2015, C++) into three pieces:
The main application in a static library
The executable which just has a main function
Unit tests which use the static .lib file so that they can import the required classes and do the unit tests on them.
Before I split it into a lib/exe/tests, the main application was just a standalone executable, and it worked perfectly fine.
Now I can't run the executable with only the main function, nor the unit tests as a certain pointer is always null. The only main difference is that I'm using a raw pointer in this example, but I was using a unique_ptr in my code (currently however I switched to the raw pointer just to make sure this following example is as accurate as possible and it didn't magically compile/run properly with the raw pointer).
The code looks very similar to the following (the extra code has been removed):
// console.hpp
extern Console *console;
The implementation cpp file (only of what is needed):
// console.cpp
Console *console = new Console();
Now in some unrelated function, this code fails due to the console pointer being a nullptr:
// some_other_file.cpp
#include "console.hpp"
// Inside some function...
console->doSomething(); // console is NULL
Again, the code I have worked perfectly fine when it was in one project. Regardless, it all compiles fine with no linking errors even though it has been broken into 3 pieces, but now that pointer is always null.
As a last interesting note, the non-pointer variables that are global and extern do work. Is this something limited to pointers/unique_ptrs?
Is this something fixable by a singleton pattern?
The clue is in this comment: "it appears other code is getting called before the main function that don't let the global variable get initialized."
The code referencing console is probably running as part of the initialization of another global and, in this case, is happening before the initialization of console. You have to be very careful to be sure that you're not depending on the order of the global initializers. You were probably getting lucky before you split the program, and now your luck has run out.
The easiest way to fix it is to use a singleton pattern. Instead of having other parts of the program directly reference the pointer, you have them call a function that returns the pointer, and that function will initialize it the first time. This ensures that it's initialized before it's used.
Here's a common pattern for it:
Console *GetConsole() {
static Console *console = new Console();
return console;
}
Now console is no longer a global variable. Everything that wants to access the console calls GetConsole instead.
The function-local static variable will be initialized the first time the function is called, and after that it just returns the value. If you have multiple threads and an older compiler, you have to do more work to protect against a possible race condition. Modern compilers should do the initialization in a thread-safe way.
Is it possible to have an OS environment variable define a macro for use in C++ code in VS2013?
For example, I would like to possibly have the environment variable DEV_LOG_DIRECTORY set, and perform the following in my C++ source file:
#ifdef DEV_LOG_DIRECTORY
logging::set_dir(DEV_LOG_DIRECTORY)
#endif
I would like to keep the actual value, and whether the macro is defined or not, out of the Visual Studio project file, so that different developers can set this how they feel and not be affected by other developer's preference. Everything I've found says to specify this in the project options, but that won't work in this case.
If there is a better way to accomplish my goal, I'm open to alternatives!
Are you sure you want to do this at compile-time? Even if it were possible (and I do not know if it is), the environment variable would only exist on the developer's machine, and would only be processed when compiling on that machine. Once you move the compiled executable to another machine, the set_dir() value would have been hard-coded into the app, but the actual target folder would likely not exist on the current machine. You probably want to instead read the environment variable from the OS that your app is actually running on, and then use it if defined, eg:
char LogDir[32767] = {0};
if (GetEnvironmentVariableA("LOG_DIRECTORY", LogDir, _countof(LogDir)) > 0)
{
logging::set_dir(LogDir);
}
else if (GetLastError() != ERROR_ENVVAR_NOT_FOUND)
{
// error reading the environment variable...
}
I'd like to get a value from a variable that's located deeply in the source code of the OpenCV library. Specifically, I'm trying to print out the value of stage_sum from the file haar.cpp. My starting point, facedetect.cpp, calls the method detectMultiScale, which then calls the function cvHaarDetectObjects, which calls cvHaarDetectObjectsForROC etc., until it finally reaches the function cvRunHaarClassifierCascadeSum, where stage_sum is calculated.
Is there a way I could get the value out to facedetect.cpp easily, without changing the declarations of all the preceding functions/methods, headers etc.? Simply trying to cout or printf the value directly in the source code hasn't given any results.
Thanks everyone for your help!
One option is simply to use a debugger.
However, if you want to do this programatically (i.e. access the variable as part of your application code), then unless the variable is exposed in the library's public interface, there are two options available:
Modify the library's source code, and recompile it.
Resort to undefined-behaviour (fiddling around with the raw bytes that make up an object, etc.).
Just to point the obvious, adding a std::cout() or printf() call inside haar.cpp won't do the trick. You need to recompile OpenCV for this changes to take effect and then reinstall the libraries on your system.