libxml for c program in ubuntu - c

I want to use libxml2 as parser for a c program on a system with ubuntu. I used the following command to install libxml2:
sudo apt-get install -y libxml2-dev
I try to compile the following file: http://xmlsoft.org/examples/reader1.c
My makefile looks like this:
xml_reader: xml_reader.o
gcc -o xml_reader xml_reader.o -lxml2 -lm
xml_reader.c: xml_reader.c
gcc -c xml_reader.c -I/usr/include/libxml2
But sadly I get the following response:
fatal error: libxml/xmlreader.h: No such file or directory
Did I miss something, which I had to do before compiling or am I even using the right -l argument?

The target of your second Makefile rule should be xml_reader.o and not xml_reader.c. Right now make is using a default rule instead which does not make use of -I/usr/include/libxml2 and thus gcc cannot find the required header.

Related

Compile hiredis in C on Mac OS X

I'm trying to compile a client using hiredis in C on Mac OS X.
I've installed hiredis with:
brew install hiredis
But still get the error:
fatal error: 'hiredis.h' file not found
My hiredis.h is however in:
/usr/local/include/hiredis/hiredis.c
How do I tell the compiler this?
I'm compiling with:
gcc test.c -o test
In your question you said hiredis.h is in /usr/local/include/hiredis/hiredis.c, which doesn't really make any sense.
Assuming you meant that your hiredis.h is in /usr/local/include/hiredis. You can do like:
gcc test.c -I/usr/local/include/hiredis -o test
Read about -I in this SO post.
UPDATE:
As mentioned by #EricPostpischil in comments, its a better idea to just include like:
#include < hiredis/hiredis.h>
I am still not sure if /usr/local/include is in default include path. If it is, well no need to do anything, just compile like:
gcc test.c -o test
and if it isn't,
gcc test.c -I/usr/local/include -o test
If you have installed hiredis with homebrew, you can see what's in the package like this:
brew ls --verbose hiredis
/usr/local/Cellar/hiredis/0.14.0/INSTALL_RECEIPT.json
/usr/local/Cellar/hiredis/0.14.0/CHANGELOG.md
/usr/local/Cellar/hiredis/0.14.0/.brew/hiredis.rb
...
...
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.dylib
/usr/local/Cellar/hiredis/0.14.0/lib/pkgconfig/hiredis.pc <--- PKG-CONFIG
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.a
/usr/local/Cellar/hiredis/0.14.0/lib/libhiredis.0.14.dylib
...
...
And, as you can see, it gives you a pkg-config file with all the settings in it that you need. So, you might as well install pkg-config and do it properly!
brew install pkg-config
Now, if you want to know the C compiler flags for hiredis, you do:
pkg-config --cflags hiredis
-D_FILE_OFFSET_BITS=64 -I/usr/local/Cellar/hiredis/0.14.0/include/hiredis
And if you want to know the linker settings, you do:
pkg-config --libs hiredis
-L/usr/local/Cellar/hiredis/0.14.0/lib -lhiredis
And so, your compile-link command becomes very simple and updates itself when you update the packages:
gcc-9 $(pkg-config --cflags --libs hiredis) -o program program.c

Why can't gcc find the include path using -I option?

here's my project tree
project
--bin
--source
----include
------foo.h
----main.c
----foo.c
but when I run gcc -I./source/include ./bin/main.exe ./source/*.c it gave me No such file or directory error.
How can I let gcc know where's the include directory?
You need to run gcc with the -o option to set the output location.
gcc -I./source/include -o ./bin/main.exe ./source/*.c

error comping c to (windows)exe on linux with mingw

I installed the mingw stuff from yaourt on arch linux but when i type
x86_64-w64-mingw32-gcc tom.c ncurses_functions.c terminal_functions.c list_functions.c -o -lpdcurses tom_windows.exe
I get:
x86_64-w64-mingw32-gcc: error: tom_windows.exe: No such file or directory
It must be something simple but I don't know what!
Try:
x86_64-w64-mingw32-gcc tom.c ncurses_functions.c terminal_functions.c list_functions.c -o tom_windows.exe -lpdcurses
The argument after -o is the output filename. In your case, you've told it to output to a file called -lpdcurses. Then you've told it to compile tom_windows.exe (as if it was a source file).
Swap the order of -o and -lpdcurses.

make error: Building 64-bit GSL in Cygwin

Continuing from here, I am trying to build 64-bit GSL using GCC in Cygwin.
The call to ./configure (CC=x86_64-w64-mingw32-gcc CFLAGS=-m64 ./configure) goes through fine, but the call to make install results, after a whole load of folders are successfully processed, in
./.libs/libgslsiman.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status
Makefile:326: recipe for target `siman_tsp.exe' failed
The full call that caused this was
Making all in siman
make2: Entering directory `/cygdrive/f/programming/c/libraries/gslCompiled/gsl-1.15/siman'
/bin/sh ../libtool --tag=CC --mode=link x86_64-w64-mingw32-gcc -m64 -o siman_tsp.exe siman_tsp.o libgslsiman.la ../rng/libgslrng.la ../ieee-utils/libgslieeeutils.la ../err/libgslerr.la ../sys/libgslsys.la ../utils/libutils.la -lm
libtool: link: x86_64-w64-mingw32-gcc -m64 -o .libs/siman_tsp.exe siman_tsp.o ./.libs/libgslsiman.a ../rng/.libs/libgslrng.a ../ieee-utils/.libs/libgslieeeutils.a ../err/.libs/libgslerr.a ../sys/.libs/libgslsys.a ../utils/.libs/libutils.a
Following advice here, I decided to run a ranlib in the ./siman/.libs directory on the libgslsiman.a file. Since that didn't work, I also tried to pack it myself using a call to ar -t libgslsiman.a.
However, this results in an identical error.
You manually forced use of the cross compiler. However, the rest of the build toolchain will still default to the 32-bit Cygwin versions instead of the 64-bit MinGW ones.
Instead of setting CC=..., pass --host x86_64-w64-mingw32 to ./configure to specify the host environment (ie where the library is going to be used).

Shared library creation in LINUX throws error

I've ten ".o" files in a directory.i want to combine them as a shared lib (.so) file.
For doing so,I am issuing following command
#gcc -shared *.o -o abc.so
but it throws following error message:
No command '-shared' found, did you mean:
Command 'gshared' from package 'gshare' (universe)
-shared: command not found
What could be the possible reason? Anything wrong with the command?
Any help ?
I agree with Chen Levy. It looks like gcc is either a stange version or not what you think it is. When I do:
gcc -shared *.o -o abc.so
I get the desired reponse. Try echo, or even:
which gcc
to try and see what's really going on. PS: I Tested on Ubuntu 10.10

Resources