Search code examples
csimgrid

Simgrid. How to divide main.c into parts?


This is an example of master-worker example. The main.c file consists of three functions and has following structure:

#include <simgrid/msg.h>
XBT_LOG_NEW_DEFAULT_CATEGORY(tuto, "all the info and defbug messages of this tutorial");

int master(int argc, char *argv[]){...}
int worker(int argc, char *argv[]){...}
int main(argc, char *argv[]){...}

I want to divide main.c into three files: main.c, worker.c, master.c. But if I will write

XBT_LOG_NEW_DEFAULT_CATEGORY(tuto, "all the info and debug messages of this tutorial") in every file it will give an error:

multiple definition of `_simgrid_log_category__tuto__constructor__'

If I will define it only once I can't use XBT_INFO in other files. How to avoid it?


Solution

  • XBT_LOG_NEW_DEFAULT_CATEGORY(tuto, "...") defines a logging category so you should only use it in one .c file. You can declare (and use) this category in another file with:

    XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tuto);
    

    Once you have called one of those macros in a file, you can use XBT_INFO(...) and friends in this file.