debugging global variable in eclipse (C/C++) - c

I wrote this sample C program:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define SIZE 10
typedef struct _sampleStruct{
int f1;
double f2;
int f3[SIZE];
}sampleStruct;
sampleStruct g_s;
int main() {
sampleStruct s;
sampleStruct zeroed = {0};
s.f1 = g_s.f1 = 1;
s.f2 = g_s.f2 = 2.0;
for (int i = 0; i < SIZE; ++i) {
s.f3[i] = g_s.f3[i] = i*10;
}
memset(&s, 0, sizeof(sampleStruct));
printf("s: %d, g_s: %lf, zeroed: %d", s.f1, g_s.f2, zeroed.f3[3]);
return 0;
}
I'm using Eclipse Mars IDE and I want to debug this code.
the s and zeroed variables are shown in the variables tab.
however, the g_s (which is a global variable) is not shown there.
is there a way for me to see its fields and how they're changing etc.?
EDIT: just figured out there's a "add global variables" button but it is grayed, so I can't press it.

ok, so after some more googling about it I found this .
tl;dr - the problem was that I was using the default GDB(DSF) process launcher. apparently this is a known issue (more details in the link above).
what you should do to solve it:
go to Run-->Debug Configurations and check at the bottom of the opened window which debug launcher you are using. if it is the GDB(DSF) then press the "Select other..." link below it. In the opened window mark "Use configuration specific settings" and then choose a different launcher (in my case it is "Legacy Create Process Launcher").
now the "add global variables should not be gray and you can click it and choose the variables you want to watch.
leaving the question just in case someone will encounter the same problem.

I hope that you find this visual description helpful.
Click the Add Global Variables icon (green dot with eye-glasses above it):
Which is located under the Variables tab:
Here is a zoom-out of both:

Related

Codeblocks C Debugging

I've got a new challenge to return the factorial of a number. Got ideas on how to do this, but the challenger has given some starting code - which is shown below.
Now this isn't how I would have started it (with my extremely limited experience!) - BUT I wasn't sure how system would grab some text & place within an int array - hence I tried running it within codeblocks, debugging and looking at the watch table. However I can't see 'num'.
So I tried copying num to num1:
int num1[30] = {0};
memset(num1[0],num[0], sizeof(num));
that doesn't seem to affect anything...
So question really is - is there something wrong with my codeblocks config (it debugs other programs and I've tried both cygwin & MiniGW) or is there another reason for this behavious?
#include <stdio.h>
#include <string.h>
void FirstFactorial(int num[]) {
// code goes here
printf("%d", num);
}
int main(void) {
// keep this function call here
FirstFactorial(gets(stdin));
return 0;
}

Because when using math it takes time to compile, and it marks me error

I'm learning to use OpenGL, and a few days ago, I wanted to use the library math.h, and at the time of execution, the compiler takes too long, and at the end I mark an error "include nested too deeply".
I do not know what happens, but I wanted to see what the problem was, and every time I include the library math.ho cmath "which is the same", the same problem happens to me.
What I did now was to do a project on the console, something easy, hello world, all good, but I included the library math.h, and again the error occurred.
It's as if that library was damaged, or not.
I would like someone to help me with these issues, since he could not solve it.
#include <iostream>
#include <math.h>
using namespace std;
int main () {
cout << "hello world";
return 0;
}
The normal code (the error):
#include <windows.h> // for MS Windows
#include <GL / glut.h> // GLUT, include glu.h and gl.h
#include <Math.h> // Needed for sin, cos
#define PI 3.14159265f
// Global variables
char title [] = "Full-Screen & Windowed Mode"; // Windowed mode's title
int windowWidth = 640; // Windowed mode's width
int windowHeight = 480; // Windowed mode's height
int windowPosX = 50; // Windowed mode's top-left corner x
int windowPosY = 50; // Windowed mode's top-left corner and
 
... etc., due to space problems.
I will not put all the code, but it is inside the Math.h library when I use it, it always happens the same, it takes time to compile, and I get these errors, and this happens when I add this.
It does not matter if I perform in OpenGl or in console as they see and the error message says "include nested too deeply".

'return accept int, return int' contains no buildables

I am new to writing code and this is my first question on this site so please forgive my ignorance if there is an obvious solution for this.
When I compile and run the code in Xcode I get a message I have never seen before. I think it has more to do with the return statement but can't seem to fix it by changing the return statement. This is an example from my text book so I would think the logic is sound. I have tried running this code on more than one computer(all mac OSX) with the same results.I have tried different IDEs including codeRunner and Xcode 4.6.3 and 3.4.? and the latest release.
Can anyone tell me what the following message means? It seems not to be a compiling error but a message in a popup window.
THE MESSAGE
"The scheme 'return accept int, return int' contains no buildables that can be built for the SDKs supported by the run destination My Mac 64–bit. Make sure your targets all specify SDKs that are supported by this version of Xcode."
Thanks in advance.
#include <stdio.h>
#include <math.h>
int cubed(int var);
float main (){
int x;
printf("Please enter an integer \n");
scanf(" %d", &x);
printf(" %d ^ 3 = %d \n",x, cubed(x));
return 1;
}
float cubed(int var){ //This line was "int cubed(int var)" before I started playing with it.
float result;
result = pow(var, 3.0);
return(result);// I get the same message if I take out this line
}
I realized the issue with the return type conflict and have changed the code to:
#include <stdio.h>
#include <math.h>
int cubed(int var);
int main (){
int x;
printf("Please enter an integer \n");
scanf(" %d", &x);
printf(" %d ^ 3 = %d \n",x, cubed(x));
return 0;
}
int cubed(int var){
float result;
result = pow(var, 3.0);
return(result);
}
I now know that it is a setting issue within Xcode.
I have attached a series of screen shots illustrating the message I was receiving, what I did, and the new error I'm getting now. I was able to get the code to run in the IDE CodeRunner but not Xcode.
-- I guess I don't have the reputation points to post images. --
But now I get this new message.
" your mac runs an osx that is lower than the minimum require for your project.--Change your project's minimum deployment target or upgrade your version of OS X."
Now what?
I think I had changed the return type in the function header when troubleshooting early on but forgot about the function prototype, things get a bit confusing when I have to jumping from one editor to another to another trying to troubleshoot. My class uses Dev C++ and sometimes this causes me some confusion.
Can anyone tell me how I could fix this new issue or how to avoid it in the future?

Is there a Linux equivalent of SetWindowPos?

A while ago I wrote a script in C that used the Windows API functions EnumWindows, SetWindowPos and SetForegroundWindow to automatically arrange windows (by title) in a particular layout that I commonly wanted.
Are there Linux equivalents for these functions? I will be using Kubuntu, so KDE-specific and/or Ubuntu-specific solutions are fine.
The best way to do this is either in the window manager itself (if yours supports extensions) or with the protocols and hints designed to support "pagers" (pager = any non-window-manager process that does window organization or navigation things).
The EWMH spec includes a _NET_MOVERESIZE_WINDOW designed for use by pagers. http://standards.freedesktop.org/wm-spec/wm-spec-1.3.html#id2731465
Raw Xlib or Xcb is pretty rough but there's a library called libwnck specifically designed to do the kind of thing you're talking about. (I wrote the original library long ago but it's been maintained by others forever.) Even if you don't use it, read the code to see how to do stuff. KDE may have an equivalent with KDE-style APIs I'm not sure.
There should be no need to use anything KDE or GNOME or distribution specific since the needed stuff is all spelled out in EWMH. That said, for certain window managers doing this as an extension may be easier than writing a separate app.
Using old school X calls directly can certainly be made to work but there are lots of details to handle there that require significant expertise if you want to iron out all the bugs and corner cases, in my opinion, so using a WM extension API or pager library would be my advice.
#andrewdotn has a fine answer there but you can do this old school as well fairly simply by walking the tree starting at the root window of the display using XQueryTree and fetching the window name with XFetchName then moving it with XMoveWindow. Here is an example that will list all the windows and if any are called 'xeyes' they get moved to the top left. Like most X programs, there is more to it and this should probably be calling XGetWindowProperty to fetch the _NET_WM_NAME extended window manager property but the example works ok as a starter. Compile with gcc -Wall -g -o demo demo.c -lX11
#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
static int
EnumWindows(Display *display, Window window, int depth)
{
Window parent, *children;
unsigned int count = 0;
int r = 1, n = 0;
char *name = NULL;
XFetchName(display, window, &name);
for (n = 0; n < depth; ++n) putchar(' ');
printf("%08x %s\n", (int)window, name?name:"(null)");
if (name && strcmp("xeyes", name) == 0) {
XMoveWindow(display, window, 5, 5);
}
if (name) XFree(name);
if (XQueryTree(display, window, &window, &parent, &children, &count) == 0) {
fprintf(stderr, "error: XQueryTree error\n");
return 0;
}
for (n = 0; r && n < count; ++n) {
r = EnumWindows(display, children[n], depth+1);
}
XFree(children);
return r;
}
int
main(int argc, char *const argv[])
{
Display *display = NULL;
if ((display = XOpenDisplay(NULL)) == NULL) {
fprintf(stderr, "error: cannot connect to X server\n");
return 1;
}
EnumWindows(display, DefaultRootWindow(display), 0);
XCloseDisplay(display);
return 0;
}
Yes, you can do this using the X Windows protocol. It’s a very low-level protocol so it will take some work. You can use xcb_query_tree to find the window to operate on, and then move it with xcb_configure_window. This page gives some details on how to do it. There’s a basic tutorial on using the library those functions come from, but you’ll probably want to Google for a better one.
It may seem daunting, but it’s not too bad. Here’s a 50-line C program that will move all your xterms 10px to the right:
#include <stdio.h>
#include <string.h>
#include <xcb/xcb.h>
void handle(xcb_connection_t* connection, xcb_window_t window) {
xcb_query_tree_reply_t *tree = xcb_query_tree_reply(connection,
xcb_query_tree(connection, window), NULL);
xcb_window_t *children = xcb_query_tree_children(tree);
for (int i = 0; i < xcb_query_tree_children_length(tree); i++) {
xcb_get_property_reply_t *class_reply = xcb_get_property_reply(
connection,
xcb_get_property(connection, 0, children[i], XCB_ATOM_WM_CLASS,
XCB_ATOM_STRING, 0, 512), NULL);
char* class = (char*)xcb_get_property_value(class_reply);
class[xcb_get_property_value_length(class_reply)] = '\0';
if (!strcmp(class, "xterm")) {
/* Get geometry relative to parent window */
xcb_get_geometry_reply_t* geom = xcb_get_geometry_reply(
connection,
xcb_get_geometry(connection, window),
NULL);
/* Move 10 pixels right */
uint32_t values[] = {geom->x + 10};
xcb_configure_window(connection, children[i],
XCB_CONFIG_WINDOW_X, values);
}
/* Recurse down window tree */
handle(connection, children[i]);
}
}
int main() {
xcb_connection_t *connection;
const xcb_setup_t *setup;
connection = xcb_connect(NULL, NULL);
setup = xcb_get_setup(connection);
xcb_screen_iterator_t screen = xcb_setup_roots_iterator(setup);
handle(connection, screen.data->root);
return 0;
}
There’s no error-checking or memory management, and what it can do is pretty limited. But it should be straightforward to update into a program that does what you want, or to turn it into a general-purpose helper program by adding command-line options to specify which windows to operate on and which operations to perform on them.
As it seems you are not looking specifically for a solution in code, but rather in a desktop environment, you need to take a look at one of the window managers that handle the window placement in such a desktop environment.
KDE's KWin's Window Attributes
Compiz (GNOME) has "Window Rules" and "Place Windows" in the CompizConfig Settings Manager application. See e.g. here
Openbox seems a lot harder to get right, although they link to a GUI tool at the bottom of this page.
The problem with using X directly is that X in itself knows nothing about your desktop environment (panels, shortcuts, etc.) and you'll have to compensate manually.
After googling for this, I'm surprised KDE is the only one that has a simple way to do this.

Obtaining List of all Xorg Displays

I would like to know how I can obtain a list of all Xorg displays on my system, along with a list of screens associated with each display. I spent some time looking through the Xlib documentation, but was not able to find a function that does what I want. Please assume that I have no other dependencies other than a POSIX-complaint OS and X (e.g., no GTK). If what I ask is not possible assuming these minimal dependencies, then a solution using other libraries is fine.
Thank you very much for your help!
The only way I know of to get a list of displays is to check the /tmp/.X11-unix directory.
Once you do that, you can use Xlib to query each display for more information.
Per example:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
#include <X11/Xlib.h>
int main(void) {
DIR* d = opendir("/tmp/.X11-unix");
if (d != NULL) {
struct dirent *dr;
while ((dr = readdir(d)) != NULL) {
if (dr->d_name[0] != 'X')
continue;
char display_name[64] = ":";
strcat(display_name, dr->d_name + 1);
Display *disp = XOpenDisplay(display_name);
if (disp != NULL) {
int count = XScreenCount(disp);
printf("Display %s has %d screens\n",
display_name, count);
int i;
for (i=0; i<count; i++)
printf(" %d: %dx%d\n",
i, XDisplayWidth(disp, i), XDisplayHeight(disp, i));
XCloseDisplay(disp);
}
}
closedir(d);
}
return 0;
}
Running the above gives me this output with my current displays/screens:
Display :0 has 1 screens
0: 3046x1050
Display :1 has 2 screens
0: 1366x768
1: 1680x1050
Never found a better way of listing X displays other than that. I'd very much like to know if any better alternative exists.
Like netcoder wrote, the problem has two distinct parts:
Connection to the X server
The process establishes a connection to an X server using XOpenDisplay(). The connection is torn down using XCloseDisplay(). netcoders code in this thread is a good example of how to do it correctly.
As netcoder mentioned, the problem is that there is no reliable way find out which X servers a process can connect to. His code checks the typical location where the X sockets are, /tmp/.X11-unix/. That approach does not work at all if the user is remotely connected, for example via SSH (with X forwarding enabled). In that case there is really only the DISPLAY environment variable (and perhaps some trickery wrt. ~/.Xauthority files).
Unfortunately, I do not know of any better method either. I personally prefer to use a per-user configuration file -- say ~/.application/displays --, where the user can list the server names the application should try to connect in the same format as the DISPLAY environment variable, in addition to the default one. It is not automatic (netcoder's code is), but this approach suits me better.
Finding out about the screens provided by an X server
XScreenCount() will return the number of screens provided by the X server the process is currently connected to. If you only need the screen dimensions, follow netcoders example. For more detailed information, use XScreenOfDisplay(Display,index) to obtain the Screen pointers; 0 <= index < XScreenCount(Display).
In C code, the macros ScreenCount() and ScreenOfDisplay() are usually a bit more efficient than the actual function calls.

Resources