Extending & Embedding Python Using C - A Module Using Linux |
Written by Mike James | ||||||
Monday, 06 May 2024 | ||||||
Page 2 of 5
GCC and VS CodeThe recommended compiler for Python extensions under Linux is the latest version of GCC. If you are using Linux you should have the GCC compiler already installed. You can check this by entering: gcc –version at the command line. If you don’t see a response either you don’t have GCC installed or it is missing from the PATH. Installing GCC on Linux is usually easy. For example, under Debian-derived systems use: sudo apt-get install build-essential gdb This installs both GCC and the GDB debugger. Once you have checked that you have GCC installed you can move on to installing VS Code. As its installation procedure changes very frequently, the best advice is to follow the current instructions on the website: https://code.visualstudio.com/. Install accepting all the defaults unless you know better. If you install the C/C++ extension you will be offered the other two as suggestions and if you install the Extension Pack you get CMake and CMake Tools automatically. CMake is the best way to keep control of a large multi-file project but you don’t need it for small single-file examples such as Hello World. If VS Code offers to configure CMake for you, select the option to ignore CMake. Before moving on to a Hello World example it is worth checking that VS Code can find GCC. Using the VS Code terminal enter: gcc –version gdb –version If either don’t work check your machine’s path environment variable or reinstall the package. Hello WorldTo create a simple example, first select the File Explorer and click the Open Folder button, navigate to a suitable location and, using right-click and New, create a new folder called CProjects and open it. Create another folder called HelloWorld inside it. You don’t have to organize your projects in this way, but it is easier if each C program has its own folder within a top-level workspace folder: Finally create a file called hello.c in the HelloWorld folder and enter: #include <stdio.h> int main(int argc, char **argv) You can compile and run the program by selecting the Run icon in the left hand panel and then selecting the compiler you want to use: The program should be compiled and you will see the message displayed in the terminal window. After this VS Code will use the details that it has stored in the tasks.json file to implement the build. If things go wrong just delete the file and try running the program again and VS Code will regenerate it for you. tasks.json is an important file because it is where you can specify the command line parameters that are passed to the compiler and the linker and these are instrumental in making the change from building a simple command line program to building a Python extension. |
||||||
Last Updated ( Tuesday, 07 May 2024 ) |