How to install libxml2 for C in OS X? - c

how to install libxml2 in OS X?
EDITED:
main.c
#include <stdlib.h>
#include <libxml2>
int main() {
printf("Hello, world!");
return 0;
}
The output I get is:
error: 'libxml2' file not found

If you have Homebrew installed, you can install libxml2 using:
brew install libxml2

To use libxml2, or any shared library, you need to...
Include the right header files in your code.
Add the path to the header files.
Add the path to the shared libraries.
Add the shared library.
The libxml2 docs aren't the best, but there are some code examples to draw from. And from that we see we need to #include <libxml/component.h> where component is whatever piece of the library you're including. For example, if you want to write XML documents, it's #include <libxml/xmlwriter.h>.
#include <stdlib.h>
#include <libxml/xmlwriter.h>
int main() {
// Just something to demonstrate we can call functions and the linking worked
xmlTextWriterPtr writer = xmlNewTextWriterFilename("example.com", 0);
// Just something to do with the variable.
printf("%p\n", writer);
}
Then you need to find the header files. OS X comes with libxml2 installed, but it's in /usr/include/libxml2. So that needs to be added to the include path with a -I/usr/include/libxml2.
The headers contain the definitions of the various functions, but the real code lies in shared libraries. Those are in the normal location, but you have to tell the compiler it to use it with -lxml2. Fortunately they're in the default location, so we don't have to add to the normal search path for shared libraries (that would be -L/some/path/).
Put it all together...
cc -I/usr/include/libxml2 -lxml2 -Wall test.c

Related

Which files to add in CMake build scripts?

This is maybe a simple question. Let's give one example of .c program:
#include <stdio.h>
#include "Example.h"
int main(){
int x;
return 0;
}
If I have some C code which includes some .h file, i.e "Example.h" and in that header file are declared some functions which are implemented in i.e. "Example.c". Now I want to build this example using CMake files. Do I need to include also "Example.c" file ("besides Example.h") in the build process(CMake file)?
In CMake you would have to define your source files for the executable you wish to create, and yes that would include Example.h and Example.c. For example:
add_executable(myprogram Example.c Example.h)
or something on the lines of:
set(SRCS "Example.c" "Example.h")
add_executable(myprogram ${SRCS})
The following link provides a good introduction to CMake:
https://cliutils.gitlab.io/modern-cmake/chapters/basics.html
Although:
CMake is smart, and will only compile source file extensions. The headers will be, for most intents and purposes, ignored; the only reason to list them is to get them to show up in IDEs.

How to include libssh to my project

I've installed libssh following the instructions and even though everything seems to be OK my compiler still returns the error "file not found" in the line "#include ". I guess it has something to do with directories or links (I have "make install" in the same folder where I downloaded it) but I don't know where should I put it so I can #include it in any project.
This is how I installed it:
I downloaded it and unzip it into the folder "libssh" on my Desktop (Mac).
Then I did
cmake -DCMAKE_INSTALL_PREFIX=/usr -DCMAKE_BUILD_TYPE=Debug ..
make
and finally:
sudo make install
Then in my program I have:
#include <libssh/sftp.h>
And XCode returns: "libssh/sftp.h file not found". I tried adding the libssh folder in the Desktop to the project, but I still have similar problems.
I guess I should install it (somehow) to the /usr/include folder, so that any project can use it (like pthread or many others), but I don't know how to do this.
If I include any other file in /usr/include it works fine (like ) but when I #include it returns file not found, even though if I cd to /usr/include/libssh the file libssh.h does exist.
This is the very simple sample code:
#include <stdio.h>
#include <pthread.h> //OK
#include <libssh/libssh.h> //Not OK, file not found.
int main(int argc, const char * argv[])
{
printf("Hello World!");
return 0;
}
In the tutorial is described how you have to link the library
You have two possibilities here:
As described you have to add those two lines to your code
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
You compile your code with the LIBSSH_STATIC flag.
gcc -DLIBSSH_STATIC test.c -o test.o
I thought that if you have the library in /usr/include the compiler will automatically link it. For instance, the pthread.h file is included properly without doing anything.
This is a system library which gets linked automatically most of the time. libssh is not. Thats why you have to be more specific on how to compile/link it.
Ive had a very similar problem several times and I have solved it by removing the ≤ ≥ symbols from around my header files and using ""s and the absolute path to the header file you're including. Now this doesn't solve your libssh install problems but it will allow you to compile just the way you have it as long as you know the absolute path of your header file and all of your header's dependencies are in the respective locations that they were inteded to look for them in. Hope this helps.

C; headers and code

I'm beginner in C programming, so I have question about basic stuff.
When I work with non-standard packages and include their headers into my projects, I'm always getting "undefined reference to function" errors. I see that header files don't contain internal code of functions, and I'm guessing that I need to link headers with the code somehow. So my question is, should I search for some libraries like dlls, which contain the functions, or should I look for C source files, and in any case, how I'm gonna link them with headers and put them all together to work in my project? I'm using CodeBlocks + MinGW.
You need to build (actually link with the library) your executable against the external library you are using which you can specify using the -L path to lib gcc flag.
e.g
gcc -L path_to_lib -llib prog.c -o executable
you can use locate lib_name to know the path of the library.
When ever you have a header file in C you will have the header file Example:
//func.h
int myfunc(int x);
then you will have a source file
Blockquote
//func.c
#include "func.h"
int myfunc(int x)
{
return x;
}
Then your source file that has main()
//main.c
#include "func.h"
int main(){
int x = 2;
x = myfunc(x);
return x;
}
in your ide you include main.c and func.c in your source files. And include func.h in your header files.
I don't use code block, but basically any ide would work this way.

Undefined references when compiling gSOAP client

I'm trying to create a client for a web service in C. I was generated C files with the wsdl2h and soapcpp2. In netbeans I'm added the generated files and the gSOAP include dir to the project's include directories.
my main file looks like this:
#include <stdio.h>
#include <stdlib.h>
#include <soapH.h>
#include <webserviceSoap12.nsmap>
int main(int argc, char** argv) {
struct soap *soap1 = soap_new();
struct _ns1__getAllCustomer *quote;
struct _ns1__getAllCustomerResponse *quote2;
if (soap_call___ns2__getAllCustomer(soap1, NULL, NULL, quote, quote2) == SOAP_OK)
printf("asd\n");
else // an error occurred
soap_print_fault(soap1, stderr); // display the SOAP fault on the stderr stream
return (EXIT_SUCCESS);
}
I copied the most of this from the gSOAP website's getting started section.
When I try to compile i get the following error:
build/Debug/MinGW-Windows/main.o: In function `main':
\NetBeansProjects\WebServiceClient/main.c:19: undefined reference to `soap_new_LIBRARY_VERSION_REQUIRED_20808'
\NetBeansProjects\WebServiceClient/main.c:22: undefined reference to `soap_call___ns2__getAllCustomer'
\NetBeansProjects\WebServiceClient/main.c:25: undefined reference to `soap_print_fault'
If I add the "soapC.c" "soapClient.c" "soapClientLib.c" files to the project I get a bunch of more undefinied reference.
What am I doing wrong? I'm using Netbeans ide with MinGW compiler on Win7. What other libraries I need or what other files should I include?
I managed to solve the problem by adding the files "soapC.c" "soapClient.c" "stdsoap.c" to the project files and in the Project propertie - Include Directories adding the files generated by soapcpp2 and the gSOAP toolkit's gsoap directory
You will need to link in the proper libraries. You will need to add the appropriate libraries using the -l switch and you will optionally need to pass the path to where these libraries reside via -L. Also, note that the libraries ending with a ++ are typically the ones you should use if you're using C++. So, your command line shoulde include at least:
For C:
gcc ... -lgsoap -L/path/to/gsoap/binaries
For C++:
g++ ... -lgsoap++ -L/path/to/gsoap/binaries
Also, depending on whether you're using additional features such as SSL, cookies etc. you will need to link these libraries in too:
g++ ... -lgsoap++ -lgsoapssl++ -L/path/...
If you're using a different toolchain, lookup the documentation for the exact switches.
I had this problem in Debian BullsEye (11), -lgsoap++ is necessary and it was solved when I added /gsoap_library_path/libgsoap++.a to g++ compiler command line.

FFMpeg sample program

I am currently learning ffmpeg tutorial of Martin Bohme Tutorial Here
and I want to compile an ffmpeg sample program using Code Block IDE but, it can't
#include <stdio.h>
#include <stdlib.h>
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
int main(int argc, char *argv[])
{
av_register_all();
return 0;
}
Please help me. How to compile it. I am using Linux (Ubuntu)
You have to tell the compiler where the header and library files are. This is done by the -I flag to tell which directories contain header files, and -L to tell which directories contains libraries. You will also need -l to tell which libraries to link with.
The flags can be used like this:
$ g++ -I/path/to/headers myprogram.cpp -L/path/to/libraries -lthelibrary
A note about libraries: On Linux (and UNIX systems) they are files with names that start with "lib" and end with the extension ".a" or ".so". When specifying the library with the -l flag you do not write those. So for a library file "libfoo.a", you only use -lfoo to link with it.
For more information about the options of gcc and g++, see http://gcc.gnu.org/onlinedocs/gcc/Invoking-GCC.html.
Edit: For an IDE like Code::Blocks there most likely is some project setting where you can add include and library directories and link libraries. Check the "Project" menu for a "Settings" or "Properties" alternative.
Edit2: See for example this FAQ where to find linker settings in Code::Blocks, the pre-processor settings should be close by.
you can try following command to compile in Linux.
gss <program-name.c>
For IDE like eclipse follow FFMPEG - Eclipse Setup Guide[Linux] official

Resources