Compile error while compiling example code #1898
-
Hello! I have just discovered Mongoose and so I am looking at integrating it into a C++ Linux project that I am working on. I copied the first example from the page https://mongoose.ws/documentation/#build-options as follows: #include "mongoose.h"
...
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
struct mg_http_serve_opts opts = {.root_dir = "."}; // Serve local dir
if (ev == MG_EV_HTTP_MSG) mg_http_serve_dir(c, ev_data, &opts);
}
...
int main() {
...
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "0.0.0.0:8000", fn, NULL); // Create listening connection
for (;;) mg_mgr_poll(&mgr, 1000); // Block forever
} However, it does not compile. Just for reference, my 'main' file is a C++ (i.e. .cpp extension, not .c extension). I receive two errors, as follows:
Noting the instructions on the build options page, I have set the compiler to use the I'm using Visual Studio Code in Linux Mint as the build environment, and my tasks.json file is set up as follows: {
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/*.cpp",
"-o",
"${fileDirname}/mt700",
"-pthread",
"-D",
"MG_ARCH_UNIX"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
} My main.cpp file is as follows: #include <stdint.h>
#include <iostream>
#include <vector>
#include <time.h>
#include <mutex>
#include <limits.h>
#include "include/globals.h"
#include "include/timestamp.h"
#include "include/colours.h"
#include "include/rtu.h"
#include "include/line.h"
#include "include/commands.h"
#include "include/mongoose.h"
Rtu rtuList[RTU_COUNT];
time_t startTime;
std::mutex mtxConsole; // note: global in scope. See extern reference in relevant cpp files
char hostName[HOST_NAME_MAX]; // holds hostname (DNS) name returned from gethostname()
std::string subsystemName; // string version of hostname (read via gethostname())
Timestamp ts;
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data)
{
struct mg_http_serve_opts opts = {.root_dir = "."}; // Serve local dir
if (ev == MG_EV_HTTP_MSG)
mg_http_serve_dir(c, ev_data, &opts);
}
int main()
{
startTime = time(NULL);
gethostname(hostName, HOST_NAME_MAX);
subsystemName.assign(hostName);
char serial_A_device[] = "/dev/ttyUSB0"; // TODO these need to be defined in a JSON file
char serial_B_device[] = "/dev/ttyUSB1";
const char *abc = "abcabc";
clearConsole();
// create RTU collection
for (auto i = 0; i < RTU_COUNT; i++)
rtuList[i].setRtuId((uint8_t)i + 1);
// create LineA and LineB objects
Line LineA(rtuList, RTU_COUNT, serial_A_device, 9600, true);
Line LineB(rtuList, RTU_COUNT, serial_B_device, 9600, false);
std::this_thread::sleep_for(100ms); // let the threads produce their console output
rtuList[0].setModuleEvented(0, true);
rtuList[0].writeModule(0, 123);
struct mg_mgr mgr;
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "0.0.0.0:8000", fn, NULL); // Create listening connection
for (;;)
mg_mgr_poll(&mgr, 1000); // Block forever
// the code below will never get called, for now...
commandLine();
LineA.quit();
LineB.quit();
std::this_thread::sleep_for(500ms);
return 0;
} I'm using the Mongoose library as copied from the .c and .h files from today (I just viewed them as raw files and copy/pasted into my project). I'm sure this is a just a newbie error on my part but if anyone could shed any light/point me in the right direction I'd be very grateful. Regards Mark edit: A blank project, without any of my code produces exactly the same results. I created a new project and tried the example code above and the error message was the same. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
I suggest you try one of the many examples in the BTW, you don't need to define MG_ARCH for Linux, it is detected automatically, just follow the examples. |
Beta Was this translation helpful? Give feedback.
I suggest you try one of the many examples in the
examples
directory, read and follow the tutorials, try modifying to get familiar with the environment, and read the docs for the functions you want to useThe compiler error message is clear, the second argument of mg_http_serve_dir() has to be a
struct mg_http_message *
and you are passing avoid *
without casting. The function docs explains you how to do that. C is different from C++.BTW, you don't need to define MG_ARCH for Linux, it is detected automatically, just follow the examples.