gcc to compile c file that ends with ".C" (Capital C) - c

A weird behaviour when I shell:
bush#ubuntu:~/CPPWorkspace/Ex12$ gcc users/dubi/justPrnit.C
Returns an error:
gcc: error trying to exec 'cc1plus': execvp: No such file or directory
But when I change justPrnit.C to justPrnit.c (with little 'c') it compiled successfully.
What's that?

error trying to exec 'cc1plus'
Because .C is assumed to be a C++ source file (cc1plus is the C++ parser backend of GCC - by the way, it seems that your local installation of GCC lacks g++ - are you using the default [incomplete] setup?).
To solve this, use the -x switch to force the language:
gcc -x c users/dubi/justPrnit.C

GCC recognises .C as C++, rather than C

Related

How to generate listing file with Clang C compiler?

I am trying to generate listing file with Clang C compiler, but I do not found any command line option for that.
Does anyone know how to generate listing file (.lst) with clang C compiler ?
Compile your file and after that run:
objdump -d -Mintel <filename>

Compiling c program with dependencies, h and h0 files

I am trying to compile the gjh solver - written in C - into an executable file in windows. It is available on netlib
I downloaded the c file and am using gcc compiler via WinGW on windows' command prompt. Trying to compile the gjh.c file directly gave me an error that says:
gjh.c:33:21: fatal error: getstub.h: No such file or directory
#include "getstub.h"
compilation terminated.
I assumed that compiling gjh.c requires the dependency getstub.h.
getstub.h is not the only dependency required, there are other dependencies, namely: arith.h, asl.h, funcadd.h, and stdio1.h. All of these files are available on the same link where I found getstub.h. However, arith.h0 and stdio1.h0 are available instead of arith.h and stdio1.h.
Are these files the same? I tried to rename the .h0 files to .h and tried to compile gjh.c, but I got this error:
collect2.exe: error: ld returned 1 exit status
Are the two files the same? If not, is there any way for me to compile the gjh solver successfully into an .exe?
If that's the only problem in compiling, try using the -I switch in gcc:
gcc -I/my/path/to/include/files -o gjh gjh.c
the -I switch hints to gcc where to find your #include files.
I am not sure about the stdio1.h. I think your approach to rename is OK but that reference to external functions such as Sprintf. You need to link with a library defining that. If you know where it comes from, use the -L and -l switch in gcc for that:
gcc -I/my/path/to/include/files -L/my/path/to/library -lnameoflibrary \
-o gjh gjh.c

Error in C program using Fedora

I get the following error when dealing with C using Fedora:
[king#localhost ~]$ gcc -o1 tempdaa.c
tempdaa.c:3:17: fatal error: queue: No such file or directory
#include <queue>
^
compilation terminated.
Any ideas on where the problem is?
gcc is generally what you use to compile C code. If you want to compile C++ code, you'd tend to use g++.
Now it's true that gcc can compile C++ if it's clear you have a C++ program but I think, from memory, that's indicated by the extension rather than the content.
Since your extension is .c rather than something like .cpp or .cc or .cxx, it will definitely think it's C code and behave accordingly.
Hence the C++ header queue will not be available to you.
My suggestion is that you name your C++ source files "correctly", or force the language type explicitly:
gcc -x c++ -o1 tempdaa.c

Compiling a C program issue in the terminal

I am trying to compile a C program in the terminal.
This is my command:
gcc -1 string -o syncing.c -o syncing
This is my result:
clang: error: no input files
I know that -1... indicates the library I used, syncing.c is the C file I am trying to compile.
What am I doing wrong with my command or is it something else?
I am only using standard libraries.
Please read up on how to use GCC, GCC command-line options and also official command-line documentation. You are telling it that syncing.c is your output file. But you want it to be your input file.
Also, I am not so sure on the -1 there. You might want to have a look at this on how to include/link external libraries. Here are more examples on that.
You probably meant something like:
gcc syncing.c -lstring -o syncing

How do I set gcc to use the file extension (.c or .cpp) to determine the correct compiler/linker?

I have a simple, representative C program, stored in a file called hello.c:
#include <stdio.h>
int main(void)
{
printf('Hello, world\n');
return 0;
}
On my Linux machine, I attempted to compile the program with gcc:
gcc hello.c
which returns an error:
undefined reference to "___gxx_personality_v0" ... etc
As has been discussed before in the context of C++, this problem arises in the linking stage, when gcc attempts to link C libraries to a C++ program, thus giving the error. In one of the answers, someone mentioned that the extension does matter, and that gcc requires the .c extension when compiling C files, and some other extension (e.g. .cpp) when compiling C++ files.
Question: How do I set gcc to use the file extension to determine which compiler to use, since gcc seems to be defaulting to C++ on my system? Specifying the language through the file extension alone doesn't seem to be enough. If I specify the language using the -x flag, gcc functions as expected.
gcc -x c hello.c
Typically, you let make decide this.
GNU Make has built in implicit rules, which automatically pick the right compiler.
Try a Makefile with these contents:
all: some_file.o some_other_file.o
And then place a some_file.cpp and some_other_file.c in the same directory, and gnu make will automatically pick the correct compiler. The linker, you may still have to provide yourself. When mixing C and C++, it's usually easiest to link with g++, like so:
program.exe: some_file.o some_other_file.o
g++ -o $# #^
This is the same as:
program.exe: some_file.o some_other_file.o
g++ -o program.exe some_file.o some_other_file.o

Resources