Using the gcc (GNU C) compiler with C99 under Windows (from CLion) - c

I am currently programming in the C language on CLion from JetBrains from a Windows computer.
I would like to use the gcc (GNU C) compiler in order to access external libraries, like BLAS.
At this point, in order to launch the hello.c program for instance, I need to go the CMakeLists.txt file and write this:
cmake_minimum_required(VERSION 3.23)
project(ProjectName C)
set(CMAKE_C_STANDARD 99)
add_executable(ProjectName hello.c)
To compile hello.c from gcc, I would need to enter cc hello.c in the gcc terminal. As CLion has no compilation problem using CMakeLists.txt, so far, so good.
Now, I want to use the dgemv_ function for matrix multiplication. I got the dgmev.f Fortran file. To execute my program, I need to use the BLAS librairy, and write cc hello.c -lblas in gcc. However, I have no idea how to link my Fortran librairy program with my C program without gcc (which I haven't).
Thus, I would like to know how to use the gcc compiler in CLion for C99 under Windows (maybe with a Makefile script ?), or, if it's not possible, how to use this Fortran librairy program without gcc.
Here you have the complete CLion project folder on Google Drive containing all the scripts I wrote about above.

Related

How to link a dynamic library (.so file) with my C Program so that I can debug my C program in Visual Studio Code

I have been provided a dynamic library (.so file) generated in the Linux platform. The file is compiled using the below command.
gcc -c -Wall -Werror -fpic -o factorial.o factorial.c
gcc -shared -o libFactorial.so factorial.o
Now, I have written a C program to call some methods inside the library (.so file) and get the desired output. The dynamic library is kept in the same directory of my C program. I need to link the library with my C program at runtime so that I can debug my C program using Visual Studio Code.
Compiler: gcc
IDE: Visual Studio Code
Platform: Linux
C Program: my_C_Program.c
Dynamic Library : libFactorial.so
When I compile and run my code using the command prompt, I am able to call the methods inside the .so file.
Commands
gcc my_C_Program.c -ldl -o my_C_Program
./my_C_Program -l libFactorial
But when I debug my code using visual studio code, I am getting errors. Do I need to link the library (statically probably ?) with my c program so that it recognizes my library. I don't know how to do that. Do I need to create a MakeFile? If yes, can you please advise how to link a library using that?
Error while debugging:
undefined reference to `dlerror.'
undefined reference to `dlsym.'
undefined reference to `dlerror.'
Please note that I don't have to generate the dynamic library. It is provided by others.

If I am building an OS, does it make sense for me to use my host OS's gcc compiler?

I am following the tutorial at https://littleosbook.github.io/ and wanted to understand whether or not what I have currently working is conceptually correct. In terms of where I am at, I am using macOS 10.15.7 for the development and was able to call a C function from the loader. The loader is in assembly. However, I used the Clang compiler (Apple clang version 12.0.0) to compile the C file in which the aforementioned C function is. Then, I compiled the C file to generate an object file and linked the .o file with loader.o
Is this how it should be done? Or should I be trying to firstly install gcc or clang inside the OS and have that compiler compile the C function for me?

Run precompiled C program

I am currently working on a program that is encrypting a text file. I made it in Turbo C++, using C language, but my main problem is that:
I need to run turboc++, in order for my .exe program to run. Does anybody here know a way to compile it and run it as stand-alone program?
TurboC++ is an obsolete compiler for an obsolete variant of C++ or of C. Use a recent compiler (such as GCC 8 or Clang 7; both are open source so freely available) for recent C11 or C++14 (or C++11) standards. Get rid of TurboC++ since it is obsolete (and is not a good compiler, compared to other existing ones).
If using GCC, you'll compile your C file foo.c using gcc -Wall -g -O foo.c -o foo. If using Clang, you'll compile with clang -Wall -g -O foo.c -o foo. Don't forget to enable all warnings and debug info. You'll get an executable foo which can be run without having the source code. That executable is specific to your operating system and to your instruction set architecture.
I need to run turboc++, in order for my .exe program to run.
With any good enough C or C++ compiler, you don't need the compiler to run the executable it is producing.
Don't confuse a compiler with the IDE or source code editor you'll use to write C or C++ source code. All C or C++ compilers I heard of are command line programs, that might be run from a terminal, an IDE, a good source code editor (such as emacs or vim).
If your source is in C++, that is bar.cc, use g++ -Wall -g -O bar.cc -o bar or clang++ -Wall -g -O bar.cc -o bar
Adapt these compilation commands (I'm giving those for Linux) to your operating system. On Windows, executables have a file path ending with .exe.
Of course, both GCC and Clang are able to compile and link a program made of several translation units. Learn to use some build automation tool, such as make or ninja. Such tools are driving compilation and linking commands.
If you are learning to program in C++, be aware that it is a very difficult programming language (you'll need years of efforts to master it). And notice that Linux is a very developer-friendly operating system, mostly made of free software whose source code you could study. That is why I recommend Linux for those learning C++ or C.
PS. If your teacher requires TurboC, I do recommend to have a polite discussion with him, suggesting to make your homework with GCC or Clang.

gcc including GMP library and osx mavericks

I am not able to compile C programs that use the gmp library under OSX mavericks, it says that it cannot find the gmp.h header file, I comiled the program like this gcc factor.c -lgmp, but it would not recognize the linker flag.
How can i compile C programs that use GMP?

Eclipse doesn't compile *.C with gcc

I am using Eclipse Kepler and running it under Win7 64-Bit. As compiler I use the gcc (4.8.1) from MinGW. Now I have the following problem:
//edit: Reformulated question to make it more clear
I have a project containing of one source-file with C-Code: main.c
This file can be compiled over 2 ways:
Start the compiler over the command-line: gcc -o main.exe main.c
Start the compiler over Eclipse by starting the normal build-routine (which also calls the gcc)
Now for some reason I want to add some C++-Code, but I still want to compile it with the gcc.
The gcc itself decides how to compile over the file extension - This means, if main.c contains C++-Code and I call gcc -o main.exe main.c it won't work. To make the compiler realize it's C++ I have to change the file-extension to somthing like .C or .cpp and then it will work.
Now back to Eclipse:
When I change my Sourcefile to main.C Eclipse interprets it as C++ File, meaning it changes the Code-Highlighting. When I now start a build process over Eclipse it just tells me
Info: Nothing to build for PROJECT
This means there is not even a call to the gcc-compiler. My guess is, that Eclipse somehow doesn't want to call the gcc, because the source-file is marked as C++-File.
//edit2: Just tried - when I have a C++-Projekt Eclipse just ignores the *.C or the *.cpp-files. I guess I have to add them manually, so they're built too ... but where?
The g++ is the compiler to compile C++ codes.
Change you compiler in you eclipse project.

Resources