You are not logged in.
Pages: 1
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
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
What commands are you running? You don't need to be in a DE to compile.
Moving to NC...
Offline
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-fileAnd 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:47Again, 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
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-fileHowever 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)
Offline
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
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
Okay, sorry. I wasn't in a console when I did it. I was in Xfce, using the file manager.
Offline
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
For each one:
bash: a.out: command not foundOffline
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.outAs small as that was. Answer found here: http://www.linuxforums.org/forum/progra … world.html
Offline
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 ![]()
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
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)
Offline
Pages: 1