LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Compiling Programs With G++ (https://www.linuxquestions.org/questions/linux-newbie-8/compiling-programs-with-g-4175659417/)

scooterboy 08-19-2019 02:15 PM

Compiling Programs With G++
 
Dear Linux Users:
Some weeks ago I started using Lubuntu 18.04, 64 bit. I installed
Code:Blocks and Allegro5 and Opengl. After compiling a program I went
to the directory where the executable was placed and clicked on it.
Linux ask what program I wanted to use to open it. Never got it to run.
I noticed that there was a question mark on the file. Never seen this
before. I then decided to compile the file from the command line. Same
problem. When using Code:blocks the file runs fine as long as I stay in
Code:Blocks. It also seems to compile fine using the command line!
I am using Code:Blocks version 16.01.
In the past I run all my programs on a 32 bit computer with Code:Blocks
version 13.12 and everything runs fine from the directory. The 64 bit
machine is where I see the problem. Can anyone help me?
Thanks for your help and everyone have a great day!
Talk to you later!

rtmistler 08-19-2019 02:52 PM

Welcome to LQ scooterboy,

I'm assuming that you've tried a very fundamental "hello world" style or program and not something very complicated?

I wonder if what your compile result is, will work on a 32-bit system.

Perhaps the project, or settings you have should be adjusted to recognize 64-bit environments. Not sure, just some guesses.

For the first hello world test, I'd create an entirely new project on the new install/system, to see if that works. Once again, if you already haven't tried this.

scooterboy 08-19-2019 03:27 PM

rtmistler:

Thanks for the reply. I did a quick helloworld.cpp program and the same thing happened.
It compiled fine, but there is a question mark on the file in the directory. It will not open.

Here is the file:

#include <iostream>
int main()
{
std::cout << "Hello, World!";
return 0;
}

This is how it was compiled:

g++ helloworld.cpp -o hello


I have been compiling C and C++ files off and on for years but NEVER seen this.

I got my start back in the days of Borland Turbo C.

A real mystery for sure. It has to be something with the 64 bit but I can't figure it out.

This is my first experience with 64 bit and may be my last!

rtmistler 08-19-2019 05:08 PM

Does it show as executable?

Do you get errors trying to run it on the command line? If so, what are they?

I've used 64-bit gcc on c programs for years, unsure what the issue is here.

What also is the output of g++ --version ?

scooterboy 08-19-2019 05:31 PM

rtmistler:
Thanks for the reply.

Does it show as executable:
Here is how the permissions are shown:

execute: Anyone

no errors from command line

output from g++ --version:

g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

rtmistler 08-19-2019 08:09 PM

What exactly does an ls -l of the file show?

No errors on command line? Did it print the output?

scooterboy 08-20-2019 06:04 AM

rtmistler:

Hello, results from ls -l:

-rwxrwxr-x 1 scott scott 8776 Aug 19 16:11 hello

There were no errors on command line.

Did not print anything on output.

BW-userx 08-20-2019 07:02 AM

Quote:

Originally Posted by scooterboy (Post 6026944)
Dear Linux Users:
Some weeks ago I started using Lubuntu 18.04, 64 bit. I installed
Code:Blocks and Allegro5 and Opengl. After compiling a program I went
to the directory where the executable was placed and clicked on it.

Linux ask what program I wanted to use to open it. Never got it to run.
I noticed that there was a question mark on the file. Never seen this
before. I then decided to compile the file from the command line. Same
problem. When using Code:blocks the file runs fine as long as I stay in
Code:Blocks. It also seems to compile fine using the command line!
I am using Code:Blocks version 16.01.
In the past I run all my programs on a 32 bit computer with Code:Blocks
version 13.12 and everything runs fine from the directory. The 64 bit
machine is where I see the problem. Can anyone help me?
Thanks for your help and everyone have a great day!
Talk to you later!

clicked on it and it did not run...

1. write some code
2. compile same code
3 run the executable from that code.
Code:

./a.out <-- executable name aka program name using the ./ for current dir.
oh, that's in a terminal btw.

scooterboy 08-20-2019 07:27 AM

BW-userx:

OK that does indeed work.

If I compile with Code:Blocks the same executable file will have a
question mark on it. Does that mean I will have to run the executable
from the command line? Never had to do that before.

BW-userx 08-20-2019 07:38 AM

Quote:

Originally Posted by scooterboy (Post 6027233)
BW-userx:

OK that does indeed work.

If I compile with Code:Blocks the same executable file will have a
question mark on it. Does that mean I will have to run the executable
from the command line? Never had to do that before.

to keep it short in linux (*nixs) command line execution is it. This is not windows. click and run.

I do not know this ? mark on it thing. ? means system does not know what it is.

when you say ? on it.. that tells me something is wrong. how may files is your program so far, if it is only one or two try using your cli to compile it then see what it does.
it's pretty straight forward for one or two files.

Code:

#include <iostream>

int main()
{
  std::cout << " HEY world..."<<std::endl;

return 0;
}

...
c++ main.cpp

..........

ls -la

-rwxr-xr-x  1 userx  userx    30016 Aug 20 07:34 a.out

..........


[userx@FreeBSD64ssd ~]$ ./a.out
 HEY world...

I'm on FreeBSD they use 'c++' and Linux uses 'g++' that is just the naming scheme of that program, no big deal. default output program name is a.out, if no name is given.

that is how it is done in Unix based, or just Unix, even before Windows even knew of itself.

you hello
Quote:

Hello, results from ls -l:

-rwxrwxr-x 1 scott scott 8776 Aug 19 16:11 hello
try
Code:

./hello
it needs the absolute path to your program because it is not in the system PATH area.
./ <-- is the shortcut way of telling system the path to program when in the same dir the program is in.

echo $PATH

rtmistler 08-20-2019 07:38 AM

@scooterboy,

I'm in front of a Linux terminal now. I have 64-bit Linux, and have had it for numerous years, I've lost count. I also have version 7.4.0 of the g++ compiler.

Your program works perfect. It compiles using the example line you've shown and it executes from the command line.

One final suggestion is to try GDB. Add -ggdb to your compile line, once completed, enter gdb with the resultant executable, set a breakpoint for line 5, the "cout" line, run it, see it hit the breakpoint, continue, and watch it complete, as well as print the output. If it can't/won't do that, or doesn't recognize any line of your source file, then seems something is wrong.

I'd recommend uninstall/re-install of your gcc toolset. Try uninstalling build-essential and re-installing it. That package contains the whole suite of tools for both C and C++ compiling.

By the way, you started all this discussing Code:Blocks, Allegro5, and Opengl. This gave me the impression that you have a project which you are building through and IDE. Hence why I recommended the course of just trying a hello world cpp file on the command line.

But given that you cannot successfully build and run on the command line, I feel there is something wrong with your install. Forget all the other libraries, environments, or tools. But I may opine that if these other libraries or tools installed "stuff" over your existing build-essential package, they may have corrupted it. Clearly your version of g++ is fine, and it matches my version word for word. But still, if one cannot do a simple compile of the most simple program, then something is wrong. The test with gdb is the best I have at this point.

Edit: And a *thing* which could be wrong could be that other environments or tools wrote over your standard libraries. That would seem to be an explanation. Maybe look at the output of "ldd hello" and see where the libraries the resultant binary is pointing to are located, if they seem to shows paths related to any of these other software packages you have installed, then that may be the key indicator which is causing you this grief. It also may be that one of those packages you installed is not 64-bit ready.

pan64 08-20-2019 08:48 AM

Quote:

Originally Posted by scooterboy (Post 6027204)
rtmistler:

Hello, results from ls -l:

-rwxrwxr-x 1 scott scott 8776 Aug 19 16:11 hello

There were no errors on command line.

Did not print anything on output.

did you try to execute that hello program? Did it print anything (or nothing?).

In general would be nice to explain better what did you try exactly and what happened exactly. Dropping in statements like above is a bit confusing.
From the other hand it is definitely not related to 32/64 bit, but something else.

Also would be nice to explain this a bit better: It compiled fine, but there is a question mark on the file in the directory. It will not open.
How did you try [to compile], how did you check [if that was successful], where is that question mark exactly, what do you mean by "will not open"?

scooterboy 08-20-2019 09:49 AM

OK, I did another test. Maybe this will help.

I changed the name of my Hello program. Loaded it into Code:Blocks
and the file ran fine. I went to the directory and the executable
had the question mark just like the other files. Would NOT open.
I then opened the terminal and the file runs fine from the terminal.

Last weekend I decided to format the drive and load everything again.
I ended up with the same problem. I have all my programs on another
computer drive and wanted to move them to this new system. It has
Ubuntu 14.04 32 bit on this drive with over 92,000 files and it works
perfect.

On another note: On Lubuntu 18.04 you can go to the directory, under
the Tools Menu there is the option 'Run a Command in Current Folder'.
If you click on this option you get another window to enter the command.
I typed in the program name, clicked OK and got this message:

Failed to execute child process
"helloworldx" (No such file or directory)

How about that, Program Manager doesn't even know the file exists, but yet
I can run it from the terminal! What in the world is going on?????????

Thanks much for all the replies. This seems to be a problem for the books.

rtmistler 08-20-2019 10:02 AM

Stop testing this using the File manager, and IDE, or from within a graphical window.

helloworldx is not your output, the output is "hello" as specified by the command line:
Code:

g++ helloworld.cpp -o hello
And by the way, at one point you mentioned a.out. That's only if you didn't specify the -o argument.

So ... sorry for asking, you've said you've programmed for years.

How good/bad are you at Linux command prompt commands as well as using GCC and G++ directly from the command line? It seems as if you're making total newbie errors here, but haven't been assuming this all along.

You write a file, helloworld.cpp, the example you provided was fine. Side note, please learn how to use [code] blocks to enclose your code. That's how it makes things in another box and preserves formatting as part of it.

Next, you demonstrated the command line to compile it correctly, thus generating an output file named "hello", so to run it from there you type ./hello{return} and it runs.

Sorry this is all I have, trying to use windows from the file manager or an IDE to try and run a simple command line program is not my object here to aid with debugging.

pan64 08-20-2019 10:12 AM

Quote:

Originally Posted by scooterboy (Post 6027287)
How about that, Program Manager doesn't even know the file exists, but yet
I can run it from the terminal! What in the world is going on?????????

Because there are rules about how to find an executable and most probably your file is just out of scope. Program Manager has no any idea where to find it.

Quote:

Originally Posted by scooterboy (Post 6027287)
This seems to be a problem for the books.

Which books?

You still did not answer and did not explain what did you try properly - hard to help you without those important details.


All times are GMT -5. The time now is 01:56 PM.