You are not logged in.

#1 2015-06-01 00:36:26

MGage
Member
Registered: 2015-06-01
Posts: 9

[SOLVED]Compilation Issues:

How does one simply compile a program that can "just run?" I used GCC as a Windows user and never once did I have issues with it. The wiki, assuming there's a proper page for it, should have something for compilation in it. If there's any other resources out there, they would be gladly appreciated.

Also, I tried compiling this in Emacs; didn't go quite so well:

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>

/* The window which contains the text. */

struct {
    int width;
    int height;
    char * text;
    int text_len;

    /* X Windows related variables. */

    Display * display;
    int screen;
    Window root;
    Window window;
    GC gc;
    XFontStruct * font;
    unsigned long black_pixel;    
    unsigned long white_pixel;
}
text_box;

/* Connect to the display, set up the basic variables. */

static void x_connect ()
{
    text_box.display = XOpenDisplay (NULL);
    if (! text_box.display) {
        fprintf (stderr, "Could not open display.\n");
        exit (1);
    }
    text_box.screen = DefaultScreen (text_box.display);
    text_box.root = RootWindow (text_box.display, text_box.screen);
    text_box.black_pixel = BlackPixel (text_box.display, text_box.screen);
    text_box.white_pixel = WhitePixel (text_box.display, text_box.screen);
}

/* Create the window. */

static void create_window ()
{
    text_box.width = 300;
    text_box.height = 300;
    text_box.window = 
        XCreateSimpleWindow (text_box.display,
                             text_box.root,
                             1, /* x */
                             1, /* y */
                             text_box.width,
                             text_box.height,
                             0, /* border width */
                             text_box.black_pixel, /* border pixel */
                             text_box.white_pixel  /* background */);
    XSelectInput (text_box.display, text_box.window,
                  ExposureMask);
    XMapWindow (text_box.display, text_box.window);
}

/* Set up the GC (Graphics Context). */

static void set_up_gc ()
{
    text_box.screen = DefaultScreen (text_box.display);
    text_box.gc = XCreateGC (text_box.display, text_box.window, 0, 0);
    XSetBackground (text_box.display, text_box.gc, text_box.white_pixel); 
    XSetForeground (text_box.display, text_box.gc, text_box.black_pixel); 
}

/* Set up the text font. */

static void set_up_font ()
{
    const char * fontname = "-*-helvetica-*-r-*-*-14-*-*-*-*-*-*-*";
    text_box.font = XLoadQueryFont (text_box.display, fontname);
    /* If the font could not be loaded, revert to the "fixed" font. */
    if (! text_box.font) {
        fprintf (stderr, "unable to load font %s: using fixed\n", fontname);
        text_box.font = XLoadQueryFont (text_box.display, "fixed");
    }
    XSetFont (text_box.display, text_box.gc, text_box.font->fid);
}

/* Draw the window. */

static void draw_screen ()
{
    int x;
    int y;
    int direction;
    int ascent;
    int descent;
    XCharStruct overall;

    /* Centre the text in the middle of the box. */

    XTextExtents (text_box.font, text_box.text, text_box.text_len,
                  & direction, & ascent, & descent, & overall);
    x = (text_box.width - overall.width) / 2;
    y = text_box.height / 2 + (ascent - descent) / 2;
    XClearWindow (text_box.display, text_box.window);
    XDrawString (text_box.display, text_box.window, text_box.gc,
                 x, y, text_box.text, text_box.text_len);
}

/* Loop over events. */

static void event_loop ()
{
    while (1) {
        XEvent e;
        XNextEvent (text_box.display, & e);
        if (e.type == Expose) {
            draw_screen ();
        }
    }
}

int main (int argc, char ** argv)
{
    text_box.text = "Hello World";
    text_box.text_len = strlen (text_box.text);
    x_connect ();
    create_window ();
    set_up_gc ();
    set_up_font ();
    event_loop ();
    return 0;
}

I would give you my output, but I cannot get GNOME to run, despite the wiki's directions.

Last edited by MGage (2015-06-02 16:33:38)

Offline

#2 2015-06-01 01:05:18

bstaletic
Member
Registered: 2014-02-02
Posts: 658

Re: [SOLVED]Compilation Issues:

Without the output we have no idea what happened. You can use wgetpaste to paste an output of a command to some pastebin service and get a link, then just post the link here.

Offline

#3 2015-06-01 01:06:41

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: [SOLVED]Compilation Issues:

What commands are you running? You don't need to be in a DE to compile.

Moving to NC...


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#4 2015-06-01 07:19:41

MGage
Member
Registered: 2015-06-01
Posts: 9

Re: [SOLVED]Compilation Issues:

Here is the process of a simple C++ program:

Firstly, the actual code. Now, I'm using a C++ "Hello World" as an example, as shown here:

#include <iostream>

int main() {
  std::cout << "Hello world!\n";
  return 0;
}

And then I (try) to compile with GCC,using the command:

gcc test.cpp -o output-file

And lastly, here is the output, albeit an error-prone output, of the "Hello World":

-*- mode: compilation; default-directory: "/crstfnos-ui/build-1/" -*-
Compilation started at Sun May 31 23:14:45

gcc test.cpp -o output
/tmp/cc42ifJt.o: In function `main':
test.cpp:(.text+0xa): undefined reference to `std::cout'
test.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/cc42ifJt.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
test.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

Compilation exited abnormally with code 1 at Sun May 31 23:14:47

Again, not sure if this is an Arch, Emacs, GCC, or operator-malfunction. My initial goal was to build a very basic X window unit but obviously that tutorial couldn't be compiled, as seen above. Since my last posting I have managed to grab ahold of Xfce4 as my DE.

Offline

#5 2015-06-01 09:48:28

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [SOLVED]Compilation Issues:

Your program is written in C++ and the functions like cout are provided by the c++ standard library. If you use a C compiler like gcc you have to explicitly link it passing the option -lstdc++:

gcc -lstdc++ test.cpp -o output-file

However you should really use a C++ compiler like g++ which links the standard library by default.

Last edited by mauritiusdadd (2015-06-01 09:51:20)


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

#6 2015-06-02 04:42:26

MGage
Member
Registered: 2015-06-01
Posts: 9

Re: [SOLVED]Compilation Issues:

Okay, I have the executable file. How do I open it?

Edit: I appreciate the (though unrelated-to-Arch-itself) support. Thanks guys.

Last edited by MGage (2015-06-02 04:43:38)

Offline

#7 2015-06-02 04:45:24

ewaller
Administrator
From: Pasadena, CA
Registered: 2009-07-13
Posts: 20,741

Re: [SOLVED]Compilation Issues:

Seriously?   You type the name of the program in a console.  If it is not in a directory that is in your path, you prefix ./ to the file name.  If you did not name the output file, it is probably called a.out


Nothing is too wonderful to be true, if it be consistent with the laws of nature -- Michael Faraday
The shortest way to ruin a country is to give power to demagogues.— Dionysius of Halicarnassus
---
How to Ask Questions the Smart Way

Offline

#8 2015-06-02 04:51:50

MGage
Member
Registered: 2015-06-01
Posts: 9

Re: [SOLVED]Compilation Issues:

Okay, sorry. I wasn't in a console when I did it. I was in Xfce, using the file manager.

Offline

#9 2015-06-02 04:59:35

MGage
Member
Registered: 2015-06-01
Posts: 9

Re: [SOLVED]Compilation Issues:

test	#test.cpp#  test.cpp~  test.o  win1.c~	 window-1.c
test.c	test.cpp    test.crtn  win1.c  win1.cpp  window-1.c~

Here's the output for two of the above files:

[root@CRSTFN-ARCH-USB build-1]# test.crtn
bash: test.crtn: command not found
[root@CRSTFN-ARCH-USB build-1]# test
[root@CRSTFN-ARCH-USB build-1]# 

Now, I tried two files: one I named test.crtn and then one simply name test, as seen above. Now trying test.o gives us this:

[root@CRSTFN-ARCH-USB build-1]# test.o
bash: test.o: command not found
[root@CRSTFN-ARCH-USB build-1]# 

All I'm asking is for a way to output the text "Hello World!" in a c++ program.

Offline

#10 2015-06-02 05:11:59

MGage
Member
Registered: 2015-06-01
Posts: 9

Re: [SOLVED]Compilation Issues:

For each one:

bash: a.out: command not found

Offline

#11 2015-06-02 05:16:33

MGage
Member
Registered: 2015-06-01
Posts: 9

Re: [SOLVED]Compilation Issues:

Okay, I found it, please close this thread:

In order for bash to recognize it as a program, you have to put a

./

in front of it.

For example:

./a.out

As small as that was. Answer found here: http://www.linuxforums.org/forum/progra … world.html

Offline

#12 2015-06-02 05:38:25

mauritiusdadd
Member
From: Benevento, Italy
Registered: 2013-10-27
Posts: 776

Re: [SOLVED]Compilation Issues:

MGage wrote:

As small as that was. Answer found here: http://www.linuxforums.org/forum/progra … world.html

Actually the answer was already given to you by ewaller wink

ewaller wrote:

If it is not in a directory that is in your path, you prefix ./ to the file name.  If you did not name the output file, it is probably called a.out

However

MGage wrote:

Okay, I found it, please close this thread

If you solved you problem then you have to mark the thread as solved by editing the first post and prepending [SOLVED] to the title https://bbs.archlinux.org/viewtopic.php?id=130309. If you instead think that your thread should be closed/removed then use the report button (located at the bottom-right edge) to contact a moderator.

Last edited by mauritiusdadd (2015-06-02 05:39:50)


About me - github

-- When you have eliminated the impossible, whatever remains, however improbable, must be the truth -- Spock | Sherlock Holmes

Offline

Board footer

Powered by FluxBB