How do we compile and run a program on the CS50 ide? - c++

So basically I am a starter in programming. I do not understand how do I compile and run the program below on Harvard's CS50 IDE.
#include<iostream>
using namespace std;
int main()
{
cout<<"HelloCS50";
}
enter image description here

Start typing in the terminal. follow each command you type with the enter key.
Use the command ls to list all the files in your directory. One of those files should be the code you just wrote and saved.
It's not a requirement, but will make it easier to have the filename end in '.cpp'. If your file does not have that, you can save the file again and add '.cpp' to the filename.
Assuming that your filename is "hello.cpp", use the command g++ hello.cpp to compile your code. This will create another file called a.out.
You can now run your code with the command ./a.out. After doing this you should see that "HelloCS50" has been printed onto the screen.

1: save your file name with .cpp extension. If your file does not have that, you can save the file again and add '.cpp' to the filename.
2: Consider that your filename is "hello.cpp", use the command make hello to compile your code. This will create another file called hello.
3: Now you can write your code with the commond ./hello. After doing this you should see the Output.

Related

Make command would modify the code indent and line feed of cpp file when I am using vscode to edit the cpp file on Ubuntu

I use make command to compile my c++ project and edit my source files with vscode on Ubuntu. But when I run the make command in the terminal, my c++ files will be modified. The original code indent of my c++ file is 4 whitespace but after compile, the code indent will be modified as 2. And the long line will be split into several short lines. Does someone know why this happens and how to prevent the compile process from modifying the source code?

Running C++ file on Terminal in Ubuntu

I'm new to C++.
I've created a C++ file with gedit in Ubuntu. But when I tried to run it on terminal, it says
no such file or directory
When I typed ls on Terminal, it shows that the c++ file was not created at all.
Where did I go wrong?
saved a file with .cpp with gedit on my desktop.
Went to terminal and typed something like g++ -o test file name.cpp.
I was then shown an error message of “no such file or directory”.
This is my code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World\n";
return 0;
}
First you have to find the directory where you saved the source file from gedit. File->Save As is one place which shows the directory, and there probably is also some "file properties" action in that menu which also shows the path. In command line use cd to move to that directory before running your compilation command.
You can use ls to see if the directory contains the files you expect, as you have done. pwd is useful for showing the full path of your current working directory. In general, it would be very useful if you did a Linux terminal tutorial or two, to get the hang of working on unix terminal command line.
Then it looks like you have spaces in your file name here: g++ -o test file name.cpp
It is a bit unclear which is which, but I presume you want this:
g++ -o test 'file name.cpp'
In unixy terminals, the shell (you probably have bash) is responsible for splitting the command line arguments, and this by default happens at whitespaces. If you have white space in single argument (like the file name here), you have to make it so that shell doesn't split that argument into two. There are several ways to do it, single quotes like above is just one, but it is a broad subject and way beyond this answer.

How do I run a command line tool from a different directory in C++

I am trying to build a C++ application that makes a bunch of command line calls on the Windows command prompt using the system() function. The C++ runs in directory BSP below. BSP contains a sub-folder BSP/XST/Files. One of the commands the application makes, needs to call a command line tool (Xilinx Synthesis tools) that needs to run in the Files directory.
BSP
|---XST
|---|---Files
Doing it manually on the command prompt I would do something similar to:
>>cd XST
>>cd Files
>>xst -h
Is there a way to call a tool in the sub-directory from the BSP directory?
I looked at this question here, but it does not work. I'm guessing because they are talking about an executable that is stored within the sub-directory whereas I am calling a command line tool (i.e. uses environment variables).
To simplify: Is there a command/option to run a command line tool in a sub-folder on the Windows command prompt? I can just emulate the statement via my C++.
As suggested by #CodyGray in the comments, my idea was to use SetCurrentDirectory. If your program is located in the BST directory and you want to run xst in the sub-folder XST\Files relative to it, then it makes sense to also use GetModuleFileName. Use this function to retrieve the path to your program and then replace the file name by your sub-folder. Finally change directory to the modified path:
#include <string>
using namespace std;
int main()
{
// Get the path to your program.
char moduleFilePath[MAX_PATH];
GetModuleFileName(NULL, moduleFilePath, MAX_PATH);
// Find the backslash in front of the name of your program.
string::size_type pos = string(moduleFilePath).rfind("\\");
// Replace the program name by your sub-folder.
string subFolderPath = string(moduleFilePath).substr(0, pos) + "\\XST\\Files";
// Change into the sub-folder relative to your program.
SetCurrentDirectory(subFolderPath.c_str());
// Execute some program in your sub-folder.
system("type test.txt");
return 0;
}
As I don't have xst, I put a text file test.txt into the sub-folder for testing purposes. The file just contains Test test test, so the program above prints out the following:
Test test test
But as suggested by #MikeVine, CreateProcess might be a smarter solution.

How do I create a make file in windows operating system and run it using cmd

I am new to makefile concept
so to try out if I am able to run and compile c files using word "make"in command prompt I made main.c (which contains main function) ,ttt.c (which contain a function void ttt(int)) , mandar.h (headerfile to include void ttt(int) function in main.c)
when I run this program in cmd using "gcc main.c ttt.c -o main && main",program gets compiled and run properly(so there shouldn't be any error in code)
Now in the same directory I made a file Makefile.txt as follow
image of makefile
Now when I type "make "in cmd following message is shown
image of cmd message
I typed everything exactly the same way as in "head first c " book
did I miss something
this is my first time to ask a question so suggestion regarding improvement of questions are also welcomed
Its not Makefile.txt, you have to name it without any extension just Makefile.
Then run the command.
FIY make by default searches for Makefile in the current working directory. You can change this default behavior by typing make -f filename into the command prompt.
You create your Makefile with some editor (emacs, vim, perhaps notepad). Beware that tab characters are significant in that Makefile and most "action" lines in it should start with a tab, not several spaces.
You then type make in some terminal or command window.

Fortran Open from current directory

I am writing a fortran program and I would like to know if it is possible to open a file from the same directory where the program itself is placed.
I am using Ubuntu 12.04 BTW.
For example, if I put the compiled program at the directory "/home/username/foo" I would like the program to open the file "/home/username/foo/bar.txt" and write "Hello!" in it.
My minimal working example is the following:
program main
implicit none
open(unit=20,file="bar.txt",action="write")
WRITE(20,*) "Hello!"
close(20)
end program main
When I compile using gfortran it opens and writes in the file "/home/username/bar.txt" no matter where I put the program file.
On the other hand, when I compile it for windows (using mingw) making a .exe file and execute it in windows it does what I want, it opens the file where the executable file is placed.
[EDIT] I just found out that if I execute the program by double clicking it, it will open the file in the program directory but when I execute it from Terminal it opens at "/home/username/", so maybe is more about the way I send the command from Terminal, currently I am doing it by the following command "/home/username/foo/myprogram".
I too am running Ubuntu 12.04 with gfortran 4.6.3, but I do not experience this. Where ever it is that I place my executable, there is bar.txt after execution.
That said, if you want a file at a specific place, then declare a character string as follows:
character(26) :: filename
filename="/home/username/foo/bar.txt"
and then open the file as
open(unit=20, file=filename)
and you are home free.
EDIT
I just noticed your edit. I imagine that you open terminal and do not cd to the location of the executable, but run the command for execution. That would indeed cause you to always have the file open in whatever folder you are currently in.