C code won't compile in mac Terminal. - c

Whenever I run $ /developer/usr/bin/gcc -v main.c -o main in Terminal, I get the following error: stdio.h: No such file or directory.
Here is the main.c file
#include <stdio.h>
int main(void){
int i;
for(i = 0; i<10;i++){
puts("Hello World!\n");
}
return 0;
}
I am pretty good with C, however I usually use xcode to compile any C command line programs. The same code runs fine in Xcode, what am I doing wrong?

I had the same problem and fixed it. Just install xCode's command line tools and it'll all work just fine!
Hope it helps! :)

You can install xcode tools with the command -
xcode-select --install
A fresh install of xcode command line tools will require 130MB of space and does require sudo access.

Related

Building library issue libudns

First of all thanks for your attention and help.
I have been trying to ./configure and build a program that use a lot of libraries, and specifically libudns. I have been installing several libraries that were needed or by apt-get or by compile, and all of them works, but libudns (which is freaking me out).
This program make use of a configure script that include the following lines of code that add the flag -ludns to the Makefile:
if [ "x$WITH_UDNS" == "xy" ]; then
mkl_lib_check "udns" HAVE_UDNS fail CC "-ludns" \
"#include <udns.h>
void *f();void *f(){return dns_init;}"
fi
When I type ./configure the script checks if all libraries are presents on the system by pkg-config and by compile, as you can see in the following snap:
As you can see, configure cannot see this library.
What I have done is try to install this package by the two different ways:
By pkg-config: sudo apt-get install libudns-dev
By compile:
git clone https://github.com/shadowsocks/libudns
cd libudns
./autogen.sh
./configure && make
sudo make install
With this two ways, I have both:
/usr/lib/x86_64-linux-gnu/libudns.so
/usr/local/include/udns.h
These paths are the same where others libraries are installed, for example PostgreSQL, which you can that is detected:
/usr/lib/x86_64-linux-gnu/libpq.so
/usr/include/postgresql/libpq-fe.h
But the result is always the same:
Does anybody knows how to link this library?
Also, I have try these other things:
Copy udns.h to /usr/include: sudo cp /usr/local/include/udns.h /usr/include/udns.h
Make a sample program only to emulate this check:
#include <stdio.h>
#include <udns.h>
struct dns_ctx* ctx;
void *f();
void * f(){return dns_init;}
int main(int argc, char** argv){
int do_open = 0;
printf("Hola, mundo\n");
f(ctx, do_open);
}
And when I try to build this program with:
gcc main.c -o hello_world -ludns
it WORKS!?!
I have also try to build this program without the -ludns flag, and it gives me the same error as before:
So, I do not understand where is the fail, considering that as you can see in the second image, the -ludns flag is present.
Thanks a lot for your time. Any advise will be welcomed.

Cross-compiled library not found by toolchain

I'm new to developing for embedded systems, but I have installed arm-linux-gnueabi-gcc via the Linux Mint package manager and managed to build a few programs successfully.
I'm currently struggling with getting a program to compile using libusb. I've done the following:
Downloaded and unpacked the libusb 1.0.20 sources from https://sourceforge.net/projects/libusb/.
Compiled and installed them using the following commands:
~/Downloads/libusb-1.0.20 $ ./configure --host=arm-linux-gnueabi --prefix=/opt/ --disable-udev
~/Downloads/libusb-1.0.20 $ sudo make
~/Downloads/libusb-1.0.20 $ sudo make install
(The reason for sudo-ing the make commands was because I encountered permission problems related to removing old files.)
Copied a small sample file from somewhere on the internet:
#include <libusb-1.0/libusb.h>
#include <stdio.h>
int main()
{
int i=0;
libusb_context **c = NULL;
i = libusb_init(c);
printf("\nusing libusb.h\n");
return 0;
}
Tried to build it and run it with gcc:
~/Desktop/libtest $ gcc libtest1.c -o libtest1 -lusb-1.0
~/Desktop/libtest $ ./libtest1
using libusb.h
However, when I try to do the same with arm-linux-gnueabi-gcc, it can't find the library:
~/Desktop/libtest $ arm-linux-gnueabi-gcc libtest1.c -o libtest1 -lusb-1.0
/usr/lib/gcc-cross/arm-linux-gnueabi/4.7/../../../../arm-linux-gnueabi/bin/ld: cannot find -lusb-1.0
collect2: error: ld returned 1 exit status
Where did I go wrong? Is there something else I need to do in order to use the library? Did I fail at compiling the library for the arm compiler? I didn't include the compiler output here since it's quite long, but there are no obvious errors. This might be a very stupid question, but I'm completely clueless.

Windows 10 can compile C programs with gcc but will not run

I installed gcc on my Windows 10 PC and can compile C code into an exe from the command prompt eg.
gcc HelloWorld.c -o HelloWorld
however when I try to run using
HelloWorld
the program will run however nothing will be output and it won't do anything
This is the C program that I compile and run that does nothing which doesn't have any errors that I can see
#include <stdio.h>
int main(void) {
printf("Hello from C!");
return 0;
}
Can someone point out whats happening?
Compile your source code by:
gcc HelloWorld.c -o ./HelloWorld.exe
and then run it by:
./HelloWorld.exe
If it still fails, you can run dir to see what's in your current directory.

Cygwin gcc compiling and running, but doesn't printf anything

I'm just starting with C and installed Cygwin with GCC compiler on Windows. I tried running this Hello World program.
#include <stdio.h>
int main(void)
{
//fflush(stdout);
//setlinebuf(stdout);
//setbuf(stdout, 0);
printf("Hello World!\n");
return 0;
}
The code compiles fine but when I try running it with ./ there is no output. I have tried to fix it using the commented lines (obviously I uncommented before running) but still had no output.
Name your source code file hello.c. In Cygwin bash shell, go to the directory where the source file hello.c is. Run gcc -o hello.exe hello.c. This will produce the executable hello.exe in the same directory. Then run ./hello.
Hope this helps.
this may be due to dll missing
add the cygwin dlls in your path, i.e.
d:\cygwin1.7.9[1]\cygwin\bin\

How can I run a C program on Mac OS X using Terminal?

I am new to C. Here is my "Hello, World!" program.
#include <stdio.h>
int main(void)
{
printf("Hello, World!\n");
return 0;
}
After I try to run it using Terminal it says:
/Users/macbook/Desktop/peng/Untitled1
-bash: /Users/macbook/Desktop/peng/Untitled1: Permission denied
Why?
First save your program as program.c.
Now you need the compiler, so you need to go to App Store and install Xcode which is Apple's compiler and development tools. How can you find App Store? Do a "Spotlight Search" by typing ⌘Space and start typing App Store and hit Enter when it guesses correctly.
App Store looks like this:
Xcode looks like this on App Store:
Then you need to install the command-line tools in Terminal. How can you start Terminal? You need to do another "Spotlight Search", which means you type ⌘Space and start typing Terminal and hit Enter when it guesses Terminal.
Now install the command-line tools like this:
xcode-select --install
Then you can compile your code with by simply running gcc as in the next line without having to fire up the big, ugly software development GUI called Xcode:
gcc -Wall -o program program.c
Note: On newer versions of OS X, you would use clang instead of gcc, like this:
clang program.c -o program
Then you can run it with:
./program
Hello, World!
If your program is C++, you'll probably want to use one of these commands:
clang++ -o program program.cpp
g++ -std=c++11 -o program program.cpp
g++-7 -std=c++11 -o program program.cpp
First make sure you correct your program:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n"); //printf instead of pintf
return 0;
}
Save the file as HelloWorld.c and type in the terminal:
gcc -o HelloWorld HelloWorld.c
Afterwards, just run the executable like this:
./HelloWorld
You should be seeing Hello, World!
A "C-program" is not supposed to be run. It is meant to be compiled into an "executable" program which then can be run from your terminal. You need a compiler for that.
Oh, and the answer to your last question ("Why?") is that the file you are trying to execute doesn't have the executable rights set (which a compiler usually does automatically with the binary, which let's infer that you were trying to run the source code as a script, hence the hint at compiling.)
This is Working in 2019
By default, you can compile your name.c using the terminal:
cc name.c
And if you need to run, just write
./name.out
To do this:
Open the terminal
Type in the terminal: nano ; which is a text editor available for the terminal. When you do this, something like this would appear.
Here you can type in your C program
Type in Ctrl + X → which means to exit.
save the file by typing in Y to save the file
Type the file name; e.g., helloStack.c (don't forget to add .c)
When this appears, type in gcc helloStack.c
And then ./a.out: this should give you your result!
For compiling a C program on your latest macOS, just type the following in the terminal after saving the file with a .c extension and on reaching the path where the file is saved:
cc yourfilename.c
Once you have checked all the errors after compilation (if any), type the following for executing the code:
./a.out
These commands are tested on macOS v10.14 (Mojave) and are working perfectly fine.
To compile a C program in macOS, simply follow the below steps
Using the cd command in terminal, go to your C program location and then type the command present below:
make filename
then type
./filename
The answer is chmod 755 hello - it makes the file executable... I had same problem on macOS, which is now solved.
nano hello.c
make hello
chmod 755 hello
Then you run it by ./hello
clang --version
Output:
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Nothing was installed. nano make (clang) chmod - all inside macOS already.
On Mac, GCC (executable gcc) is installed by default in /usr/local/bin.
To run C:
gcc -o tutor tutor.c
First you need to install a GCC compiler for Mac (google it and install it from the Internet)
Remember the path where you are storing the C file
Go to Terminal and set the path
E.g., if you have saved in a new folder ProgramC in the Document folder.
Then type this in Terminal:
cd Document
cd ProgramC
Now you can see that you are in folder where you have saved your C program (let you saved your program as Hello.c)
Now compile your program
make Hello
./hello

Resources