- How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
- Method 1: How to run C programs in Linux terminal
- Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
- Start and stop application from terminal in Linux Mint 19 / Ubuntu 18.04
- Start application from terminal — Ubuntu / Linux Mint
- Schedule application start
- Build script starting application
- Stop application by id/name
- Application stop with delay
- Auto logout from Ubuntu / Linux Mint
- Bonus tip: schedule application stop
- 6 Ways to Open a Ubuntu Application
- Open your apps the most convenient way for you
- Use the Ubuntu Launcher to Open Applications
- Search the Activities to Find an Application
- Browse the Applications Menu to Find an Application
- Use the Run Command to Open an Application
- Use the Terminal to Run an Application
- Use Keyboard Shortcuts to Open Applications
How to Write, Compile and Run a C Program in Ubuntu and Other Linux Distributions [Beginner’s Tip]
Last updated October 7, 2021 By Abhishek Prakash 21 Comments
How do you program in C on Linux? It is indeed very easy and consists of three simple steps.
Step 1: You write your program and save the file with a .c extension. For example, my_program.c.
Step 2: You compile the program and generate the object file using gcc compiler in a terminal like this:
Step 3: You run the generated object file to run your C program in Linux:
This was just the quick summary on how to compile and run C program in Linux. If you are new to either C or Linux, I’ll show these steps in detail so that you feel comfortable coding C program in Linux environment.
In fact, I’ll discuss how to run C programs in Linux terminal as well as in code editor.
Method 1: How to run C programs in Linux terminal
In order to run a C program in Linux, you need to have a C compiler present on your systems. The most popular compiler is gcc (GNU Compiler Collection).
You can install gcc using your distribution’s package manager. In Debian and Ubuntu-based Linux distributions, use the apt command:
Switch to directory where you have kept your C program (or provide the path) and then generate the object file by compiling the program:
Keep in mind that it is optional to provide the output object file (-o my_program). If you won’t do that, an object file named a.out will be automatically generated. But this is not good because it will be overwritten for each C program and you won’t be able to know which program the a.out object file belongs to.
Once you have your object file generated, run it to run the C program. It is already executable. Simple use it like this:
And it will display the desired output, if your program is correct. As you can see, this is not very different from running C++ programs in Linux.
Every time you make a change in your program, you have to compile it first and then run the generated object file to run the C program.
Method 2: How to run C programs in Linux using a code editor like Visual Studio Code
Not everyone is comfortable with command line and terminal and I totally understand that.
You can use a proper C/C++ IDE like Eclipse or Code Blocks but they are often too heavy programs and more suitable for large projects.
I recommend using an open source code editor like Visual Studio Code or Atom. These are basically text editors and you can install add-ons to compile and run programs directly from the graphical code editor.
I am using Visual Studio Code editor in this example. It’s a hugely popular open source code editor from Microsoft.
First thing first, install Visual Studio Code in Ubuntu from the software center. For other distributions, please check your Linux distribution’s package manager or software center. You may also check the official website for more information.
Start Visual Studio Code and open/create a project and create your C program here. I am using a sample Hello World program.
You must ensure that you have gcc compiler installed on your Linux system.
Next thing you would want is to use an extension that allows you to run the C code. Microsoft may prompt you for installing its own extension for C/C++ program but it is complicated to setup and hence I won’t recommend it.
Instead, I suggest using the Code Runner extension. It’s a no-nonsense extension and you can run C and C++ code easily without additional configuration.
Go to the Extensions tab and search for ‘Code Runner’ and install it.
Restart Visual Studio Code. Now, you should be able to run the C code by using one of the following way:
- Using the shortcut Ctrl+Alt+N.
- Press F1 and then select or type Run Code.
- Right click the text editor and the click Run code from context menu.
When you run the program, it is compiled automatically and then run. You can see the output in terminal that is opened at the bottom of the editor. What could be better than this?
Which method do you prefer?
Running a few C programs in Linux command line is okay but using a code editor is much easier and saves time. Won’t you agree?
I let you decide whichever method you want to use.
Like what you read? Please share it with others.
Источник
Start and stop application from terminal in Linux Mint 19 / Ubuntu 18.04
In this article we will review several scenarios how to :
- Start application from terminal — Ubuntu / Linux Mint
- Schedule application start
- Build script starting application
- Stop application by id/name in Linux Mint / Ubuntu
- Application stop with delay
- Auto logout from Ubuntu / Linux Mint
- Bonus tip: schedule application stop
Start application from terminal — Ubuntu / Linux Mint
In order to start application from terminal you need to know the name of the application which is used in the system. Sometimes the system name can differ from the application name. For example:
- Chrome — google-chrome
- Chromium — chromium-browser
- Sublime — subl
You can find where a given application is installed by:
Also there you can search for applications.
In order to start give application you need to write it’s name in the terminal:
You can pass different parameters to the application which will be opened. For example which file to be opened for Libre office:
or what web sites to be opened with chrome:
Schedule application start
Sometimes you may want to schedule a script for opening a given application. In order to improve my performance and decrease the distractions I’m scheduling my mail to be opened two times per day: 10:00 and 16:00.
The scheduling in Linux usually is done with cron jobs:
- Open terminal
- Type: crontab -e — for current user only or sudo crontab -e
- finally add your job:
Some application can be started by their name but others will need some settings. For example in order to start thunderbird and display it you need to add this line in your crontab file:
the same way if you are using bash script to start another application. For example starting LibreOffice calc:
where libre office will be started after a csv file with data is downloaded from a website.(I’ll add new article on this topic).
Build script starting application
Building special scripts to start application can be helpful in order to automate boring stuff. I have 10 scripts which are helping me to do boring stuff like:
- reporting
- getting information from the web
- sending mails
- analysing data
- starting and stoping application — obviously from the article 🙂
Below you can find several examples below:
- the first row is going to «log» each time this scripted is executed
- the second one is opening thunderbird
A more complex example where:
this example is more complex. Here is what is done:
- activate special python virtual environment
- change to a special folder
- run a web spider in order to get information from Google
- analyze and change the data
- load the download data in calc
this script is run with a cron job every day and retrieves important information for me. If I need to do the same thing I’ll need to spend 10 to 30 minutes per day.
Stop application by id/name
To stop application from the terminal by id you will need to find the id first. This can be done by this command:
10882 10869 10831 10784 10781
Another alternative is:
10882 10869 10831 10784 10781
or the more verbose one:
user 2315 0.0 0.0 15648 1152 pts/1 S+ 18:38 0:00 grep —color=auto google-chrome
user 10781 0.3 0.4 1379564 151624 ? SLl 14:57 0:47 /usr/lib/..
and then you can extract the pid-s.
After that in order to stop the application you can use:
both do similar things but the one with -9 is more «aggressive» and forcing.
In order to stop/kill application by name you can use:
this is going to kill multiple processes by name.
Application stop with delay
Now let say that you want to stop a given application with some delay: 10 minutes, 1 hour or something else. You can do with by several different ways: cron job, script, third party software. In this case we will use simple script:
this script will wait for 1 hour and then will stop the application. So you can use a script to start a given application. This application to be executed for 1 hour and finally to be stopped.
Other times examples:
Note: You can’t use:
Auto logout from Ubuntu / Linux Mint
The final problem which we are going to cover in the article is about log out of inactive users. There is application for Ubuntu / Linux Mint which can be used — autolog. You can search in the software center and install it. Or you can install it by:
Bonus tip: schedule application stop
Let say that you work a lot google chrome, slack, zoom, pycharm or any other application in Linux Mint, Ubuntu. Most of the time is really difficult to end up your working day and leave your workplace. Automation to the rescue! Below you can find a simple script and how to use in order to schedule stop for your applications in Linux:
The script will show the current date and time. Then will wait for a given period of time ( in this case 30 minutes ) and finally will kill the application.
now you can schedule this script as a cronjob by:
Источник
6 Ways to Open a Ubuntu Application
Open your apps the most convenient way for you
In this guide, you will discover a number of different ways to open an application using Ubuntu. Some of them will be obvious and some of them less so.
Not all applications appear in the launcher, and not all of them appear in the Dash. Even if they do appear in the Dash, you might find it easier to open them in other ways.
You might want to make sure Ubuntu is updated so you don’t have any issues opening applications.
Use the Ubuntu Launcher to Open Applications
The Ubuntu Launcher is on the left side of the screen and contains icons for the most commonly used applications.
- You can open one of these applications simply by selecting it.
- Right-clicking on an icon often provides other options such as opening a new browser window or opening a new spreadsheet.
Search the Activities to Find an Application
If the application doesn’t appear in the launcher the second quickest way to find an application is to use the GNOME Activities menu.
- Select Activities in the upper left corner of your screen.
- Your screen will darken to display the Activities Overview. Here, you’ll see any applications that you have minimized, the other available workspaces, and a search.
- Type the name of the application you want into the search. GNOME will show you both the apps installed on your system and the ones available for download.
For a more advanced approach of finding a program, or if you can’t find your application in the Activities search, check out our breakdown of how to use the ‘which’ command to find a program.
Browse the Applications Menu to Find an Application
If you just want to see which applications are on your computer or you know the type of application but not its name you can simply browse the GNOME applications menu.
- To browse, select the Show Applications icon on the launcher or press the Super Key + A.
- The GNOME applications menu will open up, displaying all the apps you have in your system in alphabetical order. At the bottom, you’ll find the option to toggle between All applications and your Frequent ones.
- Select an app icon to launch it.
Use the Run Command to Open an Application
If you know the name of the application you can open it quite quickly in the following way:
- Press Alt+F2 to bring up the run command window.
- Enter the name of the application. If you enter the name of a correct application then an icon will appear.
- You can run the application either by clicking on the icon or by pressing Return on the keyboard.
Use the Terminal to Run an Application
You can open an application by using the Linux terminal.
To open a terminal press Ctrl+Alt+T or follow this guide for more suggestions. If you know the program’s name you can simply type it into the terminal window. For example:
Of course, some applications aren’t graphical in nature. One example of this is apt-get, which is a command-line package manager.
When you get used to using apt-get you won’t want to use the graphical software manager anymore.
Use Keyboard Shortcuts to Open Applications
You can set up keyboard shortcuts to open applications with Ubuntu.
- To do so, open the applications menu, and search for Settings.
- Open the Settings app.
- When the window opens, locate and press Devices in the left side menu.
- Now, the menu will change to show device related options. Choose Keyboard Shortcuts.
- The main body of the window will display a huge list of current keyboard shortcuts.
- Scroll to the very bottom of the list, and select the Plus Sign (+) icon.
- Another smaller window will pop up. Enter a Name for your shortcut. For the Command, you can usually use the name of the app. Sometimes, that’s different. You can search for the path to it with which . For example: which firefox
- If you don’t know the name of the command at all, you can try:
ls -lah /usr/bin | grep -i - Then, press Set Shortcut to input the key combination. Press the combination of keys that you want to assign the shortcut to on your keyboard. Then, press Add in the upper right of the window to create your shortcut
When the launcher has been created you can set the keyboard shortcut in the same way as the other launchers.
Источник