I am new to Ubuntu and new to C programming. Now I am watching cs50 videos to understand better about C and CS all together.
I tried to install this by using these guidelines:
Debian, Ubuntu
First become root, as with:
sudo su -
Then install the CS50 Library as follows:
apt-get install gcc
wget http://mirror.cs50.net/library50/c/library50-c-5.zip
unzip library50-c-5.zip
rm -f library50-c-5.zip
cd library50-c-5
gcc -c -ggdb -std=c99 cs50.c -o cs50.o
ar rcs libcs50.a cs50.o
chmod 0644 cs50.h libcs50.a
mkdir -p /usr/local/include
chmod 0755 /usr/local/include
mv -f cs50.h /usr/local/include
mkdir -p /usr/local/lib
chmod 0755 /usr/local/lib
mv -f libcs50.a /usr/local/lib
cd ..
rm -rf library50-c-5
I used it, and I think everything went as planned, but as soon as I try to run gcc demo.c I get a fatal error message:
adder.c:2:18: fatal error: cs50.h: No such file or directory
#include <cs50.h>
So as it seems that somewhere something went wrong and I don't really know how to fix it. Could anyone guide me a little bit how to fix it or how reinstall everything that C automatically would include that library?
check in the /usr/local/include directory for the cs50.h file
If it was not there, then one or more of the shell commands failed (or was skipped).
have you tried running gcc to compile/link the demo.c file via:
gcc -c -Wall -Wextra -pedantic -Wconversion -std=gnu99 demo.c -o demo.o -I/usr/local/include
gcc demo.o -o demo -L/usr/local/lib -lcs50
If your not sure what the above two lines do, just ask.
<CS50.h> exist on the CS50 IDE only. The functions it contains are string as a type def and the get functions.
If you use char * instead of string type
and
scanf() instead of get_(type)
You do not need it.
If you do need it I would just use the IDE in the course. It's free and halfway through the course they take the training wheels off.
Use -lcs50 in the end. Also specify the file where you want the machine code output using the -o parameter.
Run this to compile the program: gcc demo.c -o demo -lcs50
Run this to execute the program: ./demo
Related
I heard that if you run Nim to generate C-code:
nim c -d: release try1.nim
Then the further generated C code can be slipped into any compiler on any operating system.
In the nimcache folder, the following is generated:
# mtry1.nim.c
stdlib_io.nim.c
stdlib_system.nim.c
try1.json
What to do next with this for compilation?
You might want to try and run nim c -d:release --genScript try1.nim. This will generate a compile_try1 script in the nimcache folder that should be able to compile the C sources generated by Nim.
nim c --cpu:mips --os:linux --compileOnly --genScript [name_project].nim
Copy everything from nimcache DIR to ur OS (which have ur toolchain (ecc, crosstoolng and etc.))
Edit script compile_[name_project].sh with necessary gcc lib
for example, mipsel-linux-gnu-gcc-9 script (If u haven't it try on Ubuntu sudo apt install gcc-9-multilib-mipsel-linux-gnu):
mipsel-linux-gnu-gcc-9 -c -w -fmax-errors=3 -IC:\nim\lib -IC:\nim\nim_practice -o stdlib_io.nim.c.o stdlib_io.nim.c
mipsel-linux-gnu-gcc-9 -c -w -fmax-errors=3 -IC:\nim\lib -IC:\nim\nim_practice -o stdlib_system.nim.c.o stdlib_system.nim.c
mipsel-linux-gnu-gcc-9 -c -w -fmax-errors=3 -IC:\nim\lib -IC:\nim\nim_practice -o #m[name_projet].nim.c.o #m[name_projet].nim.c
mipsel-linux-gnu-gcc-9 -static -o3 -Wall -fPIC -o [name_projet] stdlib_io.nim.c.o stdlib_system.nim.c.o #m[name_projet].nim.c.o -ldl
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a Makefile on my Linux Pc.I want to build and run the same program on MacOS too . How to edit the following Makefile ?
I want to compile a C program that has a header file of a graphics library EGGXProCALL JAXA "eggx.h".
I already installed Xcode ,XQuartz.
I have installed EGGX file on the following directory .
[Home#/opt/eggx]$
I have a C file in the following directory.
[Home#~/Desktop/development]$
I have checked the followings .
① gcc works fine here [Home#~/Desktop/development]$
② even the sample program to display a digital clock works ,
when the current directory is [Home#/opt/eggx]$
③ PROBLEM
when I tried to build [make] the program from [Home#~/Desktop/development]$ by using the makefile that I had in Linux.
I always get the following error message.
[Home~/Desktop/development]$ sudo make
gcc -c main.c
main.c:7:10: fatal error: 'eggx.h' file not found
#include <eggx.h>
^~~~~~~~
1 error generated.
make: *** [main.o] Error 1
[Home#~/Desktop/development]$
Here is the Makefile
# Makefile
OBJS = main
$(OBJS): $(OBJS).o
# gcc -O2 -Wall $(OBJS).c -o $(OBJS) -I/usr/local/include -L/usr/local/lib64 -leggx -lX11 -lm
gcc $(OBJS).c -o $(OBJS) -I/usr/local/include -L/usr/local/lib64 -leggx -lX11 -lm
$(OBJS).o: $(OBJS).c
gcc -c $(OBJS).c
.PHONY: clean
clean:
rm -f $(OBJS) $(OBJS).o
You must do things in the correct order and not proceed to the next step until you have correctly completed the previous step.
1. Download, and extract the library.
The download is normally done with git clone or scp to copy the source files from somewhere.
The extract (unpack from archive) is normally done with:
tar -xvf eggx-0.93r5.tar
That will normally create a new directory (with the same name as the tar-file but without the .tar extension) like:
eggx-0.93r5
2. Build the library.
Normally you need to change directory into the newly created one and run make. I gave you the Makefile last time so you need to do:
cd eggx-0.93r5
cp MAKEFILEFROMMARK Makefile
make
There should be no errors. If there are errors, you must solve them and then run:
make clean # delete any rubbish from previous failed build
make
3. Install the library.
You normally do this with:
make install
What that actually does depends on the package you are installing, but as a general rule, it will copy the header files and the libraries you just made into a "known" location, like /usr/local or /opt/package. The idea is to make all the files your own code will need available to all users of the computer by "publishing" or installing them to known locations.
4. Work out how to compile a simple C program that uses the library.
You should do the following steps in a completely different directory from where you downloaded the library to - do not mix your code with the library's code.
If your program uses eggx.h like this:
#include "eggx.h"
then you need to find where eggx.h is like this:
find /usr /opt /Users -name eggx.h
If that results in:
/path/to/somewhere/include/eggx.h
that means you must add this to your gcc command to tell the compiler how to find it:
gcc -I/path/to/somewhere/include ...
If your library is called libeggx.a, you need to find that too:
find /usr /opt /Users -name "libegg*a"
If that results in:
/path/to/somewhere/lib/libeggx.a
that means you need to add this to your gcc command to tell the linker where it is and what it is called:
gcc ... -L/path/to/somewhere/lib -leggx
If your program uses X11, you must install XQuartz on a Mac, and add the flags/switches for X11 into your compilation:
gcc ... -I /opt/X11/include -L /opt/X11/lib -lx11 ...
So, putting all that together, if your program is called program.c, you will compile and link with:
gcc program.c -o program -I/path/to/somewhere/include -I /opt/X11/include -L /opt/X11/lib -lx11 -L/path/to/somewhere/lib -leggx
and then run with:
./program
5. Make a Makefile that enshrines what you learned at (4).
That might look something like this:
EGGINC = -I /path/to/somewhere/include
EGGLIB = -L /path/to/somewhere/lib -leggx
X11INC = -I /opt/X11/include
X11LIB = -L /opt/X11/lib -lx11
$(OBJS): $(OBJS).o
gcc $(OBJS).c -o $(OBJS) $(EGGLIB) $(X11LIB)
$(OBJS).o: $(OBJS).c
gcc -I/usr/local/include $(EGGINC) $(X11INC) -c $(OBJS).c
You build your program in two steps:
Build the source file into an object file
Link the object file with libraries to create the final executable program
The preprocessor (which handles #include directives) is part of the building of the object file. So all flags that are needed for creation of the object files (like the -I option) should be present there and only there.
So the two rules could be changed as follows:
$(OBJS): $(OBJS).o
gcc $(OBJS).c -o $(OBJS) -L/usr/local/lib64 -leggx -lX11 -lm
$(OBJS).o: $(OBJS).c
gcc -I/usr/local/include -c $(OBJS).c
Of course that assumes that the EGGX library was installed in /usr/local.
I'm attempting to install the cs50 library
https://manual.cs50.net/library/#mac_os so that I can compile c code for the class on my OS X 10.10.1. Unfortunately, I'm having some problems.
Let me walk you through what I've done so far. As instructed in the above link, I entered the following commands into the bash terminal:
$ ls
cs50.c cs50.h
$ gcc -c -ggdb -std=c99 cs50.c -o cs50.o
$ ar rcs libcs50.a cs50.o
$ rm -f cs50.o
$ chmod 0644 cs50.h libcs50.a
$ sudo mkdir -p /usr/local/include
$ sudo mv -f cs50.h /usr/local/include
$ sudo mkdir -p /usr/local/lib
$ sudo mv -f libcs50.a /usr/local/lib
$ cd ..
$ rm -rf library50-c-5
After seemingly installing the library correctly, I ran gcc generate.c -o generateto compile the file. I got the following error:
$ pwd
home/Developer/pset3/find
$ ls
Makefile find.c generate.c helpers.c helpers.h
$ gcc generate.c -o generate
generate.c:17:10: fatal error: 'cs50.h' file not found
#include <cs50.h>
^
1 error generated.
$
I also got the same error when I ran gcc generate.c -o generate -lcs50
Why is the cs50.h file not being found? Is the file being installed correctly?
I searched for similar questions but others seemed to be experiencing a slightly different problem:
Harvard CS50 Library , Need Help Installing on Mac OS X
Adding a header file to Xcode
cs50 library wont link to file in cs50 appliance
After the installation of the cs50 library I added this to my ~/.bashrc file.
function make50 { gcc "$1".c -o "$1" -I /usr/local/include -L /usr/local/lib -lcs50; }
then start a new terminal or just source the ~/.bashrc file in your current terminal
source ~/.bashrc
Now lets say you have a directory with a file called generate.c in it. You should be able to run make50 generate (without the ".c") and the function should call the compiler with all the arguments needed
user#macbook:~/project$ ls
generate.c
user#macbook:~/project$ make50 generate
user#macbook:~/project$ ls
generate generate.c
You can add more library paths to your function in ~/.bashrc as needed.
I have the following code:
gcc -Wall -fno-stack-protector -O2 -g -fPIC -c ec.c
pwd
gcc -shared -Wl,-soname,libec.so.1 -o libec.so.1.0 ec.o /urs/src/soem/ethercat*.o ../soem/nicdrv.o -lc -lpthread
mv libec.so.1.0 /usr/lib/.
cd /usr/lib
ldconfig -v -n
ln -sf libec.so.1.0 libec.so
ln -sf libec.so.1.0 libec.so.1
It gives the following error when compiling:
/home/ebox/Documents/SVN/Libs/ec
gcc: error: /urs/src/soem/ethercat*.o: No such file or directory
mv: cannot stat ‘libec.so.1.0’: No such file or directory
I understand there is something wrong with the gcc command, but cannot figure out how to fix this. There are several .o files in the path that start with ethercat*.
How can I get this fixed?
The error means that there are no files that match the pattern /urs/src/soem/ethercat*.o.
Note that the first component is urs. Probably it should be usr.
when I try to compile a self written project in C with includes the mysql libraries I get this error:
gcc -c src/oDAO.c
src/oDAO.c:4:23: fatal error: my_global.h: No such file or directory
I included the my_global.h as following:
#include <my_global.h>
The error comes up, because my system copied the header files to /usr/include/mysql/ and gcc is searching for system header files only in /usr/include (without subdirectories). How can I call gcc with adding /usr/include/mysql as additional shared library root?
Here is my acutal Makefile:
all: main.o oDAO.o FileUtils.o DVDDAO.o
gcc -Llib -o oDAO main.o oDAO.o FileUtils.o DVDDAO.o -llinkedlist -lncurses `mysql_config --cflags --libs`
main.o:
gcc -c src/main.c
oDAO.o:
gcc -c src/oDAO.c
FileUtils.o:
gcc -c src/FileUtils.c
DVDDAO.o:
gcc -c src/DVDDAO.c
clean:
rm -f *.o
rm -f oDAO
rm -f *.bak
rm -f *.~
I've got the answer on my own:
Everytime i try to compile a .c file that includes a mysql library into an object file I have to compile it like that:
gcc -c DVDDAO.c `mysql_config --cflags`
Then everything works fine
Try the option -I for adding a include directory and -L for adding a libary directory.