1

I try to build a C++ project using mongoose, but I keep getting linker errors.

I tried to use the answer to the existing SO question describing similar symptoms: What is an undefined reference/unresolved external symbol error and how do I fix it? by #include C header using external C linkage:

//simple_web_server credited to Cesanta Software
#include "stdafx.h"

extern "C" {
#include "mongoose.h"
}  

static const char *s_http_port = "8000";
static struct mg_serve_http_opts s_http_server_opts;

static void ev_handler(struct mg_connection *nc, int ev, void *p) { 
   if (ev == MG_EV_HTTP_REQUEST) {
      mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
   }
}

int main(void) {
    struct mg_mgr mgr;
    struct mg_connection *nc;

    mg_mgr_init(&mgr, NULL);    // <== this causes linker error
    ...

I keep getting the following linker error:

1>------ Build started: Project: simple_web_server02, Configuration: Debug Win32 ------
1>  simple_web_server02.cpp
1>simple_web_server02.obj : error LNK2019: unresolved external symbol _mg_mgr_init referenced in function _main

The location of mongoose.h is supplied properties > VC++ > Include Directories.

I note also that omitting/including the 'extern "C" {...}' has no apparent effect.

Any help or suggestions would be much appreciated.

Community
  • 1
  • 1
SteveR
  • 61
  • 1
  • 6

1 Answers1

1

You shouldn't in principle be obliged to provide for extern "C" when including mongoose.h : this header file contains conditional compilation statements in order to ensure "C" linkage when used in a C++ project.

Apparently, you didn't include the library (lib) for the linker.

EDIT: If you didn't download/build a precompiled library, you should add the mongoose.c file to your project according to this explanation.

oshatrk
  • 499
  • 3
  • 14
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • I have now compiled mongoose.c and added a reference to the resulting .lib file in Configuration Properties>VC++ Directories>Library Directories but the same linker failures appear. ? – SteveR Apr 17 '16 at 10:48
  • @SteveR ok, and does it work ? Or do you have other linker issues ? – Christophe Apr 17 '16 at 12:18
  • Sorry if I was unclear. The same link error(s) .. unresolved external symbol .. still appear. – SteveR Apr 17 '16 at 13:44
  • 1
    What worked was to place copies of mongoose.h, mongoose.c in my project directory and then to 'add >existing resources' in Solution Explorer as Header Files and Source Files (respectively). I'm afraid that the link supplied to instructions did not explain that, or at least not to me. – SteveR Apr 17 '16 at 18:25
  • @SteveR Ok ! I think your issue could be usefull to other users of this library. I edited the title and the wording of the question to make clear that it's related to mongoose, so that other users of this library experiencing similar problems can find this post more easily. – Christophe Apr 17 '16 at 18:51