Discussion:
[libmicrohttpd] Trying to get the hellobrowser example to work
Lance Lefebure
2009-05-25 03:53:58 UTC
Permalink
Greetings. I'm relatively new to C/C++ and I'm trying to get the
hellobrowser example to work. I'm using libmicrohttpd-0.4.2 and my
environment is Kubuntu 9.04 with Kdevelop 3.5.3. I start a new C++ project
and insert the contents of /doc/examples/hellobrowser.c. That tells me I
need to get platform.h and microhttpd.h, so I grab those from /src/include/,
along with plibc.h and put them in my program's src directory. I then find
that I need MHD_config.h. I find a file with that name and a ".in" extension
in the root of the downloaded file, so I rename it and copy to my program's
src directory.



Next, when I try to build the app, I get these errors:

ts3.o: In function `main':

/home/lance/TS3/src/ts3.cpp:31: undefined reference to `MHD_start_daemon'

/home/lance/TS3/src/ts3.cpp:37: undefined reference to `MHD_stop_daemon'

ts3.o: In function `answer_to_connection(void*, MHD_Connection*, char
const*, char const*, char const*, char const*, unsigned int*, void**)':

/home/lance/TS3/src/ts3.cpp:18: undefined reference to
`MHD_create_response_from_data'

/home/lance/TS3/src/ts3.cpp:19: undefined reference to `MHD_queue_response'

/home/lance/TS3/src/ts3.cpp:20: undefined reference to
`MHD_destroy_response'



I find that I can resolve the undefined reference messages by locating each
of those functions in microhttpd.h, removing the trailing semi colon, and
adding a { } to it. I figured this was something to do with C code in C++,
although I now realize the problem is deeper.



That resolves my errors, I can built the program, it starts, but isn't
listening on the port like it should be. I then notice that the functions in
microhttpd.h don't seem to do anything. I look around and notice that in the
folder /src/daemon/ there are a bunch of .c and .h files that look useful,
but I have no idea how to use them.



I'm lost here. Any insight would be much appreciated.



Thanks,

Lance
Christian Grothoff
2009-05-25 15:04:32 UTC
Permalink
Post by Lance Lefebure
Greetings. I'm relatively new to C/C++ and I'm trying to get the
hellobrowser example to work. I'm using libmicrohttpd-0.4.2 and my
environment is Kubuntu 9.04 with Kdevelop 3.5.3. I start a new C++ project
and insert the contents of /doc/examples/hellobrowser.c. That tells me I
need to get platform.h and microhttpd.h, so I grab those from
/src/include/, along with plibc.h and put them in my program's src
directory. I then find that I need MHD_config.h. I find a file with that
name and a ".in" extension in the root of the downloaded file, so I rename
it and copy to my program's src directory.
Uh uh. There's a bunch of problems right here. You're not supposed to
directly use platform.h or MHD_config.h. The idea is that you use your build
system to generate files that produce the right #include's for your target
platform. Now, if you're not worried about broad portability, you can likely
just copy (some) of the #include's from platform.h and have them in your code
before you #include <microhttpd.h>. Using MHD_config.h.in is wrong in any
case since that file needs to be processed by configure.ac (if you take the
resutling MHD_config.h, it should work -- for the specific platform where you
compiled MHD, which is not necessarily what you want).
Post by Lance Lefebure
/home/lance/TS3/src/ts3.cpp:31: undefined reference to `MHD_start_daemon'
/home/lance/TS3/src/ts3.cpp:37: undefined reference to `MHD_stop_daemon'
ts3.o: In function `answer_to_connection(void*, MHD_Connection*, char
/home/lance/TS3/src/ts3.cpp:18: undefined reference to
`MHD_create_response_from_data'
/home/lance/TS3/src/ts3.cpp:19: undefined reference to `MHD_queue_response'
/home/lance/TS3/src/ts3.cpp:20: undefined reference to
`MHD_destroy_response'
I find that I can resolve the undefined reference messages by locating each
of those functions in microhttpd.h, removing the trailing semi colon, and
adding a { } to it. I figured this was something to do with C code in C++,
although I now realize the problem is deeper.
I doubt that it is C vs. C++, the microhttpd header takes care of the symbol
mangling. I think the problem is that you are not giving the "-lmicrohttpd"
option to your C++ compiler and it is hence not linking against the library.
Post by Lance Lefebure
That resolves my errors, I can built the program, it starts, but isn't
listening on the port like it should be. I then notice that the functions
in microhttpd.h don't seem to do anything. I look around and notice that in
the folder /src/daemon/ there are a bunch of .c and .h files that look
useful, but I have no idea how to use them.
Well, it "solved" your compile errors by you adding essentially an empty
implementation above. Do *not* touch our libmicrohttpd.h (ever, always a bad
idea), and instead LINK against libmicrohttpd. That should help.

Best,

Christian
Lance Lefebure
2009-05-26 00:13:14 UTC
Permalink
Post by Christian Grothoff
Uh uh. There's a bunch of problems right here. You're not supposed to
directly use platform.h or MHD_config.h. The idea is that you use your build
system to generate files that produce the right #include's for your target
platform. Now, if you're not worried about broad portability, you can likely
just copy (some) of the #include's from platform.h and have them in your code
before you #include <microhttpd.h>. Using MHD_config.h.in is wrong in any
case since that file needs to be processed by configure.ac (if you take the
resutling MHD_config.h, it should work -- for the specific platform where you
compiled MHD, which is not necessarily what you want).
Ah, that makes more sense now. I re-read the tutorial, ran ./configure, make, and make install, and then built the examples via command line.

Then I tried the examples in Kdevelop, but it wasn't finding the library to link. I found this can be resolved by adding "yourappname_LDADD = /usr/local/lib/libmicrohttpd.so.5" to the /src/Makefile.am file. I now have the examples working as a C++ project in Kdevelop.

The only other thing that is confusing me is that when I built the examples from command line with cc, the executables are all about 800KB. When I build it with Kdevelop, the application is about 90KB. Is the microhttpd library being put in the executable when built with cc, but just being referenced when built in C++?

Thanks,
Lance
Christian Grothoff
2009-05-27 15:34:20 UTC
Permalink
Post by Lance Lefebure
Post by Christian Grothoff
Uh uh. There's a bunch of problems right here. You're not supposed to
directly use platform.h or MHD_config.h. The idea is that you use your
build system to generate files that produce the right #include's for your
target platform. Now, if you're not worried about broad portability, you
can likely just copy (some) of the #include's from platform.h and have
them in your code before you #include <microhttpd.h>. Using
MHD_config.h.in is wrong in any case since that file needs to be
processed by configure.ac (if you take the resutling MHD_config.h, it
should work -- for the specific platform where you compiled MHD, which is
not necessarily what you want).
Ah, that makes more sense now. I re-read the tutorial, ran ./configure,
make, and make install, and then built the examples via command line.
Then I tried the examples in Kdevelop, but it wasn't finding the library to
link. I found this can be resolved by adding "yourappname_LDADD =
/usr/local/lib/libmicrohttpd.so.5" to the /src/Makefile.am file. I now have
the examples working as a C++ project in Kdevelop.
The only other thing that is confusing me is that when I built the examples
from command line with cc, the executables are all about 800KB. When I
build it with Kdevelop, the application is about 90KB. Is the microhttpd
library being put in the executable when built with cc, but just being
referenced when built in C++?
You can use "ldd" on the generated binary to see if it links against the
library or if (somehow) a static version was compiled into your binary.
However, I suspect what's really going on is that one of your binaries has
debug symbols (compiled with -g) and the other does not (and both are linked
against the shared library).

Best,

Christian
--
Christian Grothoff
Assistant Professor
Department of Computer Science
University of Denver
Alexander Antimonov
2009-05-25 23:51:27 UTC
Permalink
Greetings. I’m relatively new to C/C++ and I’m trying to get the
hellobrowser example to work. I’m using libmicrohttpd-0.4.2 and my
environment is Kubuntu 9.04 with Kdevelop 3.5.3. I start a new C++
project and insert the contents of /doc/examples/hellobrowser.c. That
tells me I need to get platform.h and microhttpd.h, so I grab those from
/src/include/, along with plibc.h and put them in my program’s src
directory. I then find that I need MHD_config.h. I find a file with that
name and a “.in” extension in the root of the downloaded file, so I
rename it and copy to my program’s src directory.
...
I’m lost here. Any insight would be much appreciated.
Thanks,
Lance
Try use Autotools build system first:

unpack libmicrohttpd-0.4.2.tar.gz
(command line)
$ cd libmicrohttpd-0.4.2
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig

More about Autotools read here:
http://en.wikipedia.org/wiki/Autotools

Built binaries of examples you can find in ./src/examples/.libs

It's better to start with src/examples/minimal_example.c

If you made a change to minimal_example.c source,
run from root libmicrohttpd-0.4.2 directory:
$ cd ./src/examples/
$ make
Loading...