Help with C Program Executable - Linux - c

I just recently made the move to Linux, and now looking to program on it as well.
However, for some reason I cannot get an executable to work.
Here's what I've done:
Downloaded Code::Blocks
Made a new Console Project in Workspace 1 with C source.
Added a getchar() before return(0);
Ran and Compiled - Which works perfectly INSIDE Code::Blocks
Went to the bin/release folder in which the file is saved, tried double clicking, right clicking and selecting: open, open with, tried using terminal to run the name of my program. I copied the folder URL, and then name of the file.. I just can't seem to get the created file to execute!
In windows it made a .exe, I know there is no ending (?) in linux. (Could be wrong).
I'm currently running Ubuntu 11.04.

Most Linux distributions don't include the current directory in the PATH variable that determines where to search for executables. Try opening a terminal, changing to the bin/release directory and explicitly qualifying your executable for the current directory:
./myprogram
This is in contrast to Windows, where you can simply type "myprogram.exe".

You might need to grant your program permission to run as an executable:
sudo chmod +x yourProgram

In the terminal emulator, go to (cd) the folder where the executable is created.
Type ./programname
Where programname is the name of the executable file
(./ tells the shell to look in the current directory for the program to run)

Related

Impossible to run a.out file through Android terminal

I compiled and linked a file on Android (ARM). I have got a a.out file and iwant to execute it on Android (ARM) using android terminal via the command (./). It's written "operation not permitted". I even try "chmod" but it's the same thing.
How can I fix it?
I have found it. The problem is that we can not execute .out file on Android sdcard. So, as i am using an Android terminal emulator called Termux, i copied the a.out file in the Termux home directory or a subdirectory called "Storage" via the command line "cp". In these two directories, i successfully execute the a.out file.
On Sdcard, "a.out" file only have "read" and "write" properties, not, "execute".

Compile C source file using Command Prompt

I am having trouble while using the codeblocks-16.01mingw-setup.exe - installed in a file whose path does not contain spaces - when trying to compile at a comand prompt. A beginner's guide recommended using the following line in command prompt:
gcc cards.c -o cards
for the source file named cards.c (on my desktop). This gives the error
'gcc' is not recognised as an internal or external command, operable program or batch file.
When trying to figure this out, I have found out that you can drag and drop files in your command prompt and it specifies their path. Doing this with the gcc.exe taken from F:\Programare\Codeblocks\MinGW\bin\gcc.exe and adding the c source file as C:\Users\dream\Desktop\cards.c gives the error
as.exe - System error: The program can't start because libintl-8.dll is missing from your computer. Try reinstalling the program to fix this problem.
I've reinstated my Codeblocks to no avail. I've also tried matching the paths of the compiler with the source file, bringing the source code where the compiler was, again to no avail.
Please help me understand the issue. I must say that the file named libintl-8.dll is right there where gcc.exe is, trying to add it in the command prompt gives some syntax error.
gcc is trying to find that dll file in the working directory, so you need to cd into that directory cd /d F:\Programare\Codeblocks\MinGW\bin.
Dynamic-Link Library Search Order (Windows)
The directory specified by lpFileName.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
You have to add F:\Programare\Codeblocks\MinGW\bin\ to your users or systems PATH environment variable.
Once you have done this you can simply type
gcc cards.c -o cards

Running an executable file through Cygwin

I've recently opened up Head First C and I was having an issue with one of the programs. (I'm using Windows)
I saved a cards.c file in my Documents folder under a folder named Practice and navigated to that folder within Cygwin
I compiled using gcc cards.c -o cards and it compiled with no errors, so I tried to type cards to run the exe file but I got -bash: cards: command not found.
I read through several stackoverflow answers and tried multiple suggestions such as ./a exe, /a exe, cards.exe, a cards, etc/bin/cards and more, but I could only run the file with /cygdrive/c/Me/Documents/Practice/cards.exe
I'm somewhat new with Cygwin so I'm wondering if there is something I missed or a better way to go about this? I'm really looking to learn. On a side note I'm wondering if copying cygwin1.dll from the cygwin bin file to the folder containing the exe file will help, but I don't know how to access the bin file to copy it.
the output file will be named cards, not cards.exe
there are a few different ways to execute that file, here are some of those ways.
a) . cards <-- notice the leading '. '
b) at the cygwin command line in the directory where 'cards' is located:
chmod cards 777
cards
c) from the directory where cards is located:
./cards
Suggest the b) method since it is a permanent fix

Simple way to execute C program without IDE

I am new to C. What is the most simple way to execute a C program without an IDE?
I created my first C program und it runs flawlessly using my IDE Netbeans on Mac OS X. Now I need to make that program be "executable without IDE on Windows".
Also the requirement is only "Windows", no specific version. Which means I might have to support XP, Vista, 7, 8.
What is the most simple way to do so?
As soon as you have built your app, it is a program you can run outside the IDE. You just need to find it. :) In NetBeans this will usually be under your project directory in a subdirectory called "dist". There will be an executable file in there which you can copy out to wherever you want.
Download MinGW here: http://sourceforge.net/projects/mingw/files/latest/download?source=files
and install it.
Then go to cmd (In Windows 8 - Windows key+R and type cmd in the window opened. In Windows 7 - search >> run >> cmd).
After installing it and entering cmd type in the Command Prompt window (CMD) cd C:/MinGW/bin (when MinGW is on C drive which is the default path). After doing so type the following command: gcc -o X.exe Y.c when X is the name of the output file (what name you want the .exe file to be and Y is the name of the C file that must be inside of the BIN folder (C:/MinGW/bin by default). It should be compiled smoothly if it compiled in your IDE and then the .exe file would be in the BIN folder named X.exe (when X is what you typed in the CMD). Afterwards, you can close your CMD and do with the .exe file whatever you like.

Using Cygwin to Compile a C program; Execution error

I'm enrolled in a masters computer science course. The course is using C and the instructor wants us to use Cygwin to compile programs if we are using windows.
I've downloaded and installed Cygwin and I've ensured that I've installed the GCC compiler.
But I don't know where to go from here. I need to compile a single source file that has a basic include.
#include <stdio.h>
Lets assume the file is on my desktop (it is not, but for the sake of argument). How do I navigate to the desktop from the bash shell? I assume once I've navigated to the correct location in bash, I simply execute:
gcc myProgram.c -o myProgram
Update: Following different instructions posted below, I was able to compile the program; I thank you for that. But when I execute the resulting binary I get the following. How can I compile or execute this program so I don't get the error? Again, thank you.
This application has failed to start because cygwin1.dll was not found. Re-installing the application may fix this problem.
when you start in cygwin, you are in your $HOME, like in unix generally, which maps to c:/cygwin/home/$YOURNAME by default. So you could put everything there.
You can also access the c: drive from cygwin through /cygdrive/c/ (e.g. /cygdrive/c/Documents anb Settings/yourname/Desktop).
Regarding your updated question about the missing cygwin1.dll.
From the Cygwin terminal check,
ls /usr/bin/cygwin1.dll
If it is not present (I doubt that), your installation is not properly done.
Then, check your path with,
echo $PATH
This will give : separated list of paths. It MUST contain /usr/bin. If you find that missing add it with,
export PATH=/usr/bin:$PATH
Finally,
I hope you are using Cygwin from the cygwin terminal (the little green+black icon installed with Cygwin), or MinTTY (if you installed that).
And, you have not moved the compiled EXE to a different machine which does not have Cygwin installed (if you do that, you will need to carry the cygwin1.dll to that machine -- keep it in the same folder as the compiled EXE).
Just to summarize, here are some commands that navigate to a directory and compile code using Cygwin and Windows Vista:
Start a Cygwin shell.
At the prompt, use cd to change to the appropriate directory:
$ cd /cygdrive/c/Users/nate/Desktop
Use ls to list the files in the directory:
$ ls
prog.c
Use the gcc command to compile a file in this directory:
$ gcc prog.c -o prog
If you don't see any errors, you should be able to run the resulting program:
$ ./prog
Update:
For the "Cygwin1.dll not found" error, I like Nik's answer. You might also check out this related post about cygwin1.dll not found, which suggests adding c:\cygwin\bin\ to your Windows PATH.
There are instructions on how to change the Windows PATH variable for Windows XP, and on Vista I think it's similar.
Go to Control Panel -> System
Select Advanced System Settings
Click on the Advanced tab
Click on Environment Variables
Under System Variables, find the Path entry and click Edit
Add c:\cygwin\bin to the list, making sure to separate it from any previous items with a semicolon
Look for (that is, cd to)
/cygdrive/c/
that will usually be your C:\
Also look at Using Cygwin, the Lifehacker introduction (June/2006) and, this biomed page at PhysioNet.
Regarding the cygwin1.dll not found error, a solution I have used for at least 8 years is to add the Cygwin bin directories to the end of my %PATH% in My Computer -> Properties -> Advanced -> Environment Variables. I add them to the end of the path so in my normal work, they are searched last, minimizing the possibility of conflicts (in fact, I have had no problems with conflicts in all this time).
When you invoke the Cygwin Bash Shell, those directories get prepended to the %PATH% so everything works as intended in that environment as well.
When not running in Cygwin shell, my %PATH% is:
Path=c:\opt\perl\bin; \
...
C:\opt\cygwin\bin; \
C:\opt\cygwin\usr\bin; \
C:\opt\cygwin\usr\local\bin;
This way, for example, ActiveState Perl's perl is found first when I am not in a Cygwin Shell, but the Cygwin perl is found when I am working in the Cygwin Shell.
If you are not comfortable with bash, you can continue to work in a standard windows command (i.e. DOS) shell.
For this to work you must add C:\cygwin\bin (or your local alternative) to the Windows PATH variable.
With this done, you may:
1) Open a command (DOS) shell
2) Change the directory to the location of your code (c:, then cd path\to\file)
3) gcc myProgram.c -o myProgram
As mentioned in nik's response, the "Using Cygwin" documentation is a great place to learn more.
If you just do gcc program.c -o program -mno-cygwin it will compile just fine and you won't need to add cygwin1.dll to your path and you can just go ahead and distribute your executable to a computer which doesn't have cygwin installed and it will still run. Hope this helps
Windows path C:\src under cygwin becomes /cygdrive/c/src
Compiling your C program using Cygwin
We will be using the gcc compiler on Cygwin to compile programs.
1) Launch Cygwin
2) Change to the directory you created for this class by typing
cd c:/windows/desktop
3) Compile the program by typing
gcc myProgram.c -o myProgram
the command gcc invokes the gcc compiler to compile your C program.
You might be better off editing a file inside of cygwin shell. Normally it has default user directory when you start it up. You can edit a file from the shell doing something like "vi somefile.c" or "emacs somefile.c". That's assuming vi or emacs are installed in cygwin.
If you want to file on your desktop, you'll have to go to a path similar (on XP) to "/cygwindrive/c/Documents\ and\ Settings/Frank/Desktop" (If memory serves correctly). Just cd to that path, and run your command on the file.
Cygwin is very cool! You can compile programs from other systems (Linux, for example), and they will work. I'm talking communications programs, or web servers, even.
Here is one trick. If you are looking at your file in the Windows File Explorer, you can type "cd " in your bash windows, then drag from explorer's address bar into the cygwin window, and the full path will be copied! This works in the Windows command shell as well, by the way.
Also: While "cd /cygdrive/c" is the formal path, it will also accept "cd c:" as a shortcut. You may need to do this before you drag in the rest of the path.
The stdio.h file should be found automatically, as it would be on a conventional system.
This file (cygwin1.dll) is cygwin dependency similar to qt dependency.you must copy this file and similar files that appear in such messages error, from "cygwin/bin" to folder of the your program .Also this is necessary to run in another computer that have NOT cygwin!

Resources