How to execute a C file through cmd - c

In my current example, I have a C file called 'Main.c' which converts and prints US pounds into UK stones/UK pounds/kilos, and the variable US pounds is currently defined by the user's input (scanf). The file itself is executed through Visual Express 2013; however, I want to change this so that I can navigate to the C file and execute the file directly through the commmand prompt (cmd) whiles passing a value to define US pounds.
I understand that this is required in main:
void main(int argc, char *argv[])
And I know how to navigate to the C file through the command prompt:
>cd Directory/To/The/File
However this is where I get stuck; I don't know how to execute the C file. I have researched into this and found several examples, such as using 'gcc' and 'cc', but the system doesn't recognise these commands. None of the materials that I have found fully explains what exactly I have to do in order to accomplish what I am trying to do; do I have to install something, or am I using the wrong commands, is it possible what I am trying to do, what excactly do I have to do?

Broadly speaking, you need to do the following:
Compile the program using a compiler to generate an executable.
Run the executable file.
When you "run the program in Visual Studio", it is doing all of those steps for you.
I don't know much about command line compiling in Windows, but I believe msbuild is the name of the compiler which Visual Studio uses. Look into that, and you'll see how to compile. Compilation will then generate the executable, which you just type into the command line to run.
EDIT: I found an article which suggests that cl is a C compiler command available on the command line.
Here is the relevant excerpt:
At the command prompt, specify the cl command together with the name of your source file—for example, cl simple.c—and press Enter to compile the program. The cl.exe compiler generates an .obj file that contains the compiled code, and then runs the linker to build an executable program that has the name of your source file, but has an .exe file name extension—for example, Simple.exe.

You cannot cd to the file, only to the directory. The cd command means "change directory".
You cannot execute a C file, it's just a text file and your computer doesn't know how to run it. You need to compile it first (this is what Visual Studio does). There should be an EXE file somewhere close, you need to find it. It is the binary, executable, form of your C program which you can run directly from the command line.

Related

How to execute a C file in notepadqq for Linux?

I'm trying to run a C file using Notepadqq in Manjaro Gnome edition, but when I try running my script by going to the Run command it opt up a windows that says Special Placeholders
enter image description here
This should be corrected fairly easily via the graphical user interface. No command or file modification should be necessary.
In Nautilus you can right click on the file to open.
I'm trying to run a C file using Notepadqq in Manjaro Gnome edition, but when I try running my script by going to the Run command it opt up a windows that says Special Placeholders enter image description here
To run a C program you need to compile it first, generate an executable and then run it as a normal system command. You cannot run it directly from Notepad++ because as such, it is not still executable. This is generally done with a program called a C compiler (which you don't mention if you have one or not) I figure that you are on a Windows computer, so the variety and availability of C compilers makes it impossible to continue giving you advice. You need to install a compiler, learn how to use it, and then you'll know how to make your C program executable in the system.
I recommend you to read a good programming book in C, like "The C Programming Language" from Brian Kernighan & Dennis Ritchie, to know how to compile a program and your operating system's manual to know how to execute a program.
Edit
Oh, sorry, you said on linux. You have to save your source file (with some name ended in .c) then compile it with something like
$ cc my_hello.c -Wall -o my_hello
where my_hello.c is the name you gave to the C source file, -Wall makes the compiler to be more verbose in explaining your C programming errors and -o my_hello specifies the compiler to output the executable command in a file called my_hello.
(I have represented the system prompt as $ and the screen cursor as _, you don't need to key those symbols) and then
$ my_hello
Hello$ _
(as you didn't end the line with \n, the next system prompt will appear next to your program's last message) to get it printed correctly, just modify your program to appear as
#include <stdio.h>
int main()
{
printf("Hello\n");
/* ^^-- insert the '\n' there */
}

Creating CLI apps in C

I am wondering how to make a CLI app in C that "constantly runs". What i mean by that us for example i want to just open terminal and type a keyword and a function in a program executes. Like what "ls" is.
When you type ls it lists contents of the current dir. Likewise i want to make a program that when compiled it executes a certant stuff given the keyword is invoken. I dont want to run the executable with ./example, but rather have the command always available.The compiler i use is gcc. I have read that object file needs to be created but i dont know how to use that.
Thanks
What i mean by that us for example i want to just open terminal and type a keyword and a function in a program executes.
Your program doesn't have to "constantly run" in order for you to be able to invoke it without specifying the path... you just have to make sure that the program is located in one of the directories in your PATH, or conversely, that your PATH environment variable includes the directory where your program is located.
So let's say you want to compile hello.c into a command called hello. Here's the code:
#include <stdio.h>
int main(void)
{
printf("Hello, world!\n");
return 0;
}
So you'd compile that like:
> gcc -o hello hello.c
Now you should have an executable file called hello in your current directory, and you can run it like:
> ./hello
But you just want to type hello anywhere, right? So, you can add the current directory to your PATH environment variable, e.g.:
> export PATH=$PATH:$PWD
That adds the value of PWD, an environment variable that contains the current directory, to PATH, which is an environment variable that contains a list of directories where the shell will look for executable programs.
If you want to make that change permanent, you'll need to modify one of the scripts that runs when you start up whatever shell you're using. That's a little beyond the scope of this answer, and there's plenty of advice about how to set up your PATH online, so I'll leave that to you.
Another option, instead of adding the directory that contains hello to your PATH, is to move hello to one of the directories already listed in PATH. You can see the full list by doing this:
> echo $PATH
You haven't said what OS you're using, but if it's anything Unix-like there's probably a /usr/local/bin listed in there. .../bin directories generally hold executable programs, and /usr/local is the directory sub-tree where local additions to the OS go. So you could put your program in /usr/local/bin, and then (assuming /usr/local/bin is in your PATH), hello would always be available.

How come when I try to compile my C program by making a file named for the program it creates an application for it?

I once tried to compile a C program I made that was for a chess game (thanks to YouTube's Bluefever Software for the tutorial), but when I went to compile the program, I executed this line of code:
C:\TDM-GCC-64\>gcc Chess/chess.c Chess/init.c -o chess
The compiling worked (there were no syntax errors or anything), but when I got to my file directory, I saw this (circled in blue):
An unexpected application (but there were no viruses!):
How did this happen? It may had something to do with the line I was compiling, but what is the "intel" behind this?
It is normal for the compiler to generate an application!
What is surprising is the location for the executable, it should have been generated in the parent directory:
C:\TDM-GCC-64\> gcc Chess/chess.c Chess/init.c -o chess
The explanation is interesting:
You are using the Windows operating system, where the filenames are case insensitive.
You instructed gcc to generate the executable into chess, but this is the name of the Chess directory. In this case, gcc generates the executable in the named directory and gives it a name that is the basename of the first source file chess.c -> chess.
Furthermore, the application name really is chess.exe in Windows, but the default setting for the file manager is to not display file extensions. This is a very unfortunate choice. I suggest you change this setting in the Windows/File Explorer Options window to always show file extensions. This will allow you to distinguish chess.c, chess.exe and chess.h more easily.
You have a Makefile in the Chess directory, you should use the make command to build the executable:
C:\TDM-GCC-64\> make -C Chess
Or simply cd to the Chess subdirectory and type:
C:\TDM-GCC-64\Chess> make
That's the file you told the compiler to make.
The -o option to gcc is the output file. In this case, you told it to create an executable file named chess. And that's exactly what was created.
The compiler is automatically creating an executable file while compiling.

C to NASM conversion

I'm trying to find a way to convert simple C code to NASM assembly. I have tried using objconv and downloaded and unzipped and built it since I am using a MAC; however, it doesn't seem to be working. I keep getting "-bash: objconv: command not found". Does anyone know another way or can help me solve the -bash error.
Bash is the program that takes the words you type in a terminal and launches other programs. If it is reporting an error, it is because it cannot find the program you want to run (at least in this case).
You need to either find a pre-packaged installation of objconv, or you need to do the work to "integrate" your copy of objconv yourself.
If you can identify the executable you want to run (probably called objconv) you need to add that to your path. The easiest way (if it is just for you) is to verify that your ~/.bashrc or ~/.bashprofile has a line that looks something like
PATH=$PATH:${HOME}/bin
Don't worry if it doesn't look exactly the same. Just make sure there's a ${HOME}/bin or ~/bin (~ is the short version of ${HOME}).
If you have that then type the commands
cd ~/bin
ln -fs ../path/to/objconv
and you will create a soft link (a type of file) in your home binary directory, and the program should be available to the command line.
If you create the file, and nothing above has any errors, but it is not available to the command line, you might need to set the executable bit on your "real" (not link) copy of objconv.
If this doesn't work, by now you should be well primed for a better, more specific question.
If you have gcc installed, try gcc -masm=intel -S source.c to generate assembly files in a syntax very similar to that of MASM.

Merge C program and VHDL bitstream via "make" (i.e. using a Makefile)

I am trying to test the VHDL AVR8 soft processor found on Gadget Factory on a Digilent Nexys II (Spartan 3E) Development board. The project includes a Makefile for compiling a C (or other) software program and merging it with the FPGA bitstream so there is no need to resynthesize the HDL with every iteration of the software.
When I execute 'make' I get the following error associated with data2mem:
Merging Verilog Mem file with Xilinx bitstream: main.bit
data2mem -bm bin/top_avr_core_v8_bd.bmm -bt bin/top_avr_core_v8.bit -bd main.mem -o b main.bit
process_begin: CreateProcess(NULL, data2mem -bm bin/top_avr_core_v8_bd.bmm -bt bin/top_avr_core_v8.bit -bd main.mem -o b main.bit, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [main.bit] Error 2
I am executing 'make' in the same directory containing the VHDL project files, and I even have a blank 'main.bit' file in the directory.
Does anyone have any ideas about what's going on here? Does my blank 'main.bit' file need to be formatted a certain way or placed in a different location?
The following is a link to my Makefile:
Makefile
Other information to note: I'm new to using Makefiles in general, let alone for the specific purpose of merging software with an FPGA bitstream file. Also, I am attempting this on a Windows 7 machine in command prompt.
Thanks in advance.
Edit: Here's a link to the AVR8 soft processor on Gadget Factory, and here's the AVR8 source.
Offhand I'd say make cannot invoke the data2mem program. Do you have such a program on your system? Is the directory containing it in your PATH variable? Does it run properly? For example, can you type in that command line yourself at the command prompt and have it work properly?
When you say a "blank" main.bit, I assume you mean a "fully populated except for the memory that I want to put the program into" main.bit... otherwise nothings going to work!
It sounds like you do not have data2mem on your path - are you sure you are running your makefile from a command window/shell which includes the xilinx paths?
On Windows, there is a specific icon for this. Alternatively you can open any old command prompt and run the settings32.bat (or settings64.bat) file from within the Xilinx install folder to get set up. Or on linux, you can source the appropriate .sh/.csh file in your shell.

Resources