Basic Compiling...

Pages: 12
I'm not new to C++, but I just recently decided to switch to Linux (Windows 8 sucks...) and I'm having trouble running compiled programs.

Is there any recommended way to build them? Also, I can't seem to run the binaries... They are 64 bit (i am running 64 bit architecture)... I can't figure this out and I've searched google for the answer far and wide. (it reads: "Cannot execute binary file!")

So far, It seems that the binary won't initiate it's own command-line based terminal, like shells, and so I guess using the system() function (forbidden on Windows) is not such a security hole? Anyway...

I would like to start porting some programs, so I need some function equivilants (I tried to find them...):

-conio.h (if needed, was told I do not, and Ncurses is too bloated for my needs, unless you can tell me how to clear the screen without initiating the whole graphics)

-windows.h: Specifically- filesystem operations, time/date, and color(if no Ncurses)

Also, should I link statically or Dymaically? If statically, how can I link statically?

Thanks for your help, I really apreciate it!
Last edited on
How are you trying to run the program? What output filename are you specifying when compiling?
I think you can still build and run 32bit programs while on 64bit OS.
Also, system() should be avoided on ANY platform.
Linux's drawing system is different from windows's.
Windows has its own headers for drawing, playing sounds and so on.
Linux uses X11 for drawing, has different devices and drivers for audio and so on again.
As you may understand there's no 1:1 equivalent.
You should seriously use Ncurses while on linux tho.

Linking dynamically/statically is not an issue you should take into account right now, and i'm almost sure you can ONLY link dynamically.
Dependencies will be (probably) downloaded while installing it, and they will be placed in a shared system directory.

The name of the executable is simply "program".

main.cpp
common.cpp
common.o

command:

A small shell i whipped up:

1
2
3
4
#/bin/sh

echo compiling C++ using program -pedantic-errors -Wall
g++ -o program -pedantic-errors -Wall $1 $2 $3


calling of the shell:

gccp main.cpp common.cpp common.h

and I end up with a binary.

Also, I have tried PDcurses... I hated it. I was more like a graphics library than a basic input/output library. Can you tell me how I can use it without usng the whole darn thing, or do I have to learn how to initiate windows, panels, blablabla... (in other words, learn all of it...)?

when I try to run the new binary (it's properties say that it can be executed) through the command line:

./program

I get this:

Unable to run binary file.
(or somthing along those lines...)
Last edited on
GCC is used for C.
I don't know about GCCP.
But try:
g++ main.cpp common.cpp -o main -pedantic-errors -Wall

Properties say it can be executed because it's a file flag, just like a "Read Only" flag on Windows.

Also, don't insert your .h files in the command line.
They don't need to be compiled on their own.
They will be included from the .cpp files.
Last edited on
gccp is just the name of the shell file I wrote.... it could just as well be named domylaundry...

Since I have it added to the environment path variable, I don't have to use it's full path to call it.

still not working...

now it is:

g++ $1 $2 $3 -o program -pedantic-errors -Wall which should equate to what you wrote.

also, echo $1 $2 $3 yeilds "main.cpp common.cpp cursesclass.cpp", so it should work.
(the reason it's done like this is so I don't have to create a new one for every project I create, I can just run it from I command line that has the path in it's environment variable)

I have:

main.cpp
common.cpp
cursesclass.cpp (i wrapped up curses...)

I get an executable; same results: can't run it. It just acts like i pressed enter without any commands...
Last edited on
Does it run now, or does it still say Unable to run binary file?
In case it doesn't say it, what's in your main.cpp ?
Otherwise, you may want to download Code::Blocks to make sure your development set-up is good (I think installing Code::Blocks will also download the other [software] requirements, if there are any).
... my previous post ansers your first question... (no...)

it compiles fine. I open up the terminal in LINUX, "./program" and it acts as though I simply pressed enter with no commands...

What would code::blocks do for me that I can't already do? I have the compiler, I have an IDE, what's code::blocks going to do?
Well, installing it will install the dependencies (G++, ...) and probably the debug libraries, unless you already had them.

If it acts just like you just pressed enter, show us your main.cpp file?
Unless you are using another compiler g++ is what you need, and it is default for many distros and IDEs. If you want to write a small program try making a script. I usually have a ./compile.sh

g++ -o myProgram.run *.cpp *.h

g++ -o myProgram.run *.cpp

This should compile all of the .cpp and .h files in the current directory. Than you can run your new application with ./myProgram.run

Of course this is a sloppy way of doing it, research make files, using g++ to build object files and linking them, or use an IDE like Code::Blocks. Seeing the build log of Codeblocks will give you an idea how g++ works.
Last edited on
@vasilenko93: please read all posts in a topic before responding. It has been previously stated that you should not compile header files like that.
@L B: Sorry about that, it was weird writing that myself because I don't remember including header files into command line ether.
Eh, hehe, what? Why would you suggest something you haven't tried yourself? XD
I know how G++ works, it just another program to pass a bunch of arguments to...

Also, I figured out the problem and I fell like a HUGE MORON for over-seeing this:

main() called nothing and returned without calling my test function... DOY... And hence the root of what used to be my "troubles"...

Anyway, it's running now, and now I can't get curses to clear the infernal screen... The getch() function works lovely though! :) I might switch to it in windows, because I wrapped it in a class I wrote to handle the initialization and destruction of curses for me.

1
2
ccurses().cls()
ccurses().gkey()


nice huh?

cls() doesn't work though. it just acts as though it's never called...

1
2
3
4
5
void ccurses::cls()
{
	erase();
	refresh();
}


I think this is correct... I hope...

Also, I would apppreciate it if I could get some documentation. I tried searching myself, and couldn't find much. I need somthing that lists all of the functions/variables.
ccurses().cls()
ccurses().gkey()

I would make them static methods so you can call them like
1
2
ccurses::cls();
ccuses::gkey();


Also, ncurses has a clear() function so why not use that?

Here's some documentation http://invisible-island.net/ncurses/man/ncurses.3x.html
I tried clear and it didn't work...

1
2
clear();
refresh();


but nothing...

EDIT1: clear() only works if I don't refresh()... And then it won't display anything afterwards...
Last edited on
Works for me, try this toy app
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <ncurses.h>
#include <unistd.h>

int main()
{
	initscr();
	for(int i = 0; i < 10; ++i)
	{
		clear();
		refresh();
		std::cout << "hello world " << i << std::endl;
		sleep(1);
	}
	getch();
	endwin();
}

How do I compile a simple C program?
Start your favourite text editor and type in your source code. For example, I may use pico:
pico hello.c
and type in the Kerningham and Richie (the creators of "c") intro example of a C program:

#include <stdio.h>
void main(void) {
printf("hello world\n");
}

If you're using the command line, don't use that script file, create a makefile.

Let's say you have 3 source files: main.cc, a.cc and b.cc and you compile it with that script
1
2
3
4
#/bin/sh

echo compiling C++ using program -pedantic-errors -Wall
g++ -o program -pedantic-errors -Wall $1 $2 $3


That translates to:
1
2
3
4
5
6
7
8
9
10
11
PROG = main
SRC = main.o a.o b.o
CXXFLAGS = -pedantic-errors -Wall

all: ${PROG}

clean:
    rm ${SRC} ${PROG}

${PROG}: ${SRC}
    ${LINK.cc} -o $@ $^

Call it makefile
Indent using tabs, not spaces.

When you run make, it'll generate:
g++ -pedantic-errors -Wall   -c -o main.o main.cc
g++ -pedantic-errors -Wall   -c -o a.o a.cc
g++ -pedantic-errors -Wall   -c -o b.o b.cc
g++ -pedantic-errors -Wall    -o main main.o a.o b.o
This is what I get:

command:

./makefile


Result:

./makefile: line 1: PROG: command not found
./makefile: line 2: SRC: command not found
./makefile: line 3: CXXFLAGS: command not found
./makefile: line 5: all:: command not found
./makefile: line 7: clean:: command not found
rm: missing operand
Try `rm --help' for more information.
./makefile: line 11: ${LINK.cc}: bad substitution


er... I did what you said...
Pages: 12