Search code examples
c++cmacrosincludesyslog

Why the linker does not see my function (define macro to substitute the syslog)?


I try to substitute the syslog function in such a manner:

#include <cstdio>
#include <cstdarg>

void syslog_printf(int level, char* format, ...)
{
  va_list params;
  va_start(params, format);
  vprintf(format, params);
  printf("\n");
  va_end(params);
}

#define syslog(...) syslog_printf(__VA_ARGS__)

#include <modbus/modbus.c>

This is a listing of my modbus.cpp file. The modbus.c is there too, but I can't modify it because it used in several places. The modbus.cpp compiled right but the linker reports that it can't find the syslog_printf function.

The question is simple: why?

EDIT:

make -k all 
Building file: ../modbus.cpp
Invoking: GCC C++ Compiler
g++ -std=c++0x -DDEBUG -I../../include -O0 -g3 -Wall  -c -fmessage-length=0 -MMD -MP -MF"modbus.d" -MT"modbus.d" -o "modbus.o" "../modbus.cpp"
Finished building: ../modbus.cpp

Building target: fire
Invoking: GCC C++ Linker
g++  -o "fire"  ./main.o ./modbus.o  
./modbus.o: In function `error_treat(modbus_param_t*, int, char const*)':
../../include/modbus/modbus.c:144: undefined reference to `syslog_printf'
./modbus.o: In function `modbus_send(modbus_param_t*, unsigned char*, int)':
../../include/modbus/modbus.c:379: undefined reference to `syslog_printf'
./modbus.o: In function `receive_msg(modbus_param_t*, int, unsigned char*, int*)':
../../include/modbus/modbus.c:482: undefined reference to `syslog_printf'
../../include/modbus/modbus.c:484: undefined reference to `syslog_printf'
../../include/modbus/modbus.c:511: undefined reference to `syslog_printf'
./modbus.o:../../include/modbus/modbus.c:524: more undefined references to `syslog_printf' follow
collect2: error: ld returned 1 exit status
make: *** [makefile:46: fire] Error 1
make: Target 'all' not remade because of errors.

EDIT:

If I try to switch the modbus.cpp to the modbus.c, I get this:

make -k all 
Building file: ../modbus.c
Invoking: GCC C Compiler
gcc -DDEBUG -I../../include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"modbus.d" -MT"modbus.d" -o "modbus.o" "../modbus.c"
../modbus.c:13:21: error: conflicting types for ‘syslog_printf’
 #define syslog(...) syslog_printf(__VA_ARGS__)
                     ^
../modbus.c:4:6: note: previous definition of ‘syslog_printf’ was here
 void syslog_printf(int level, char* format, ...)
      ^~~~~~~~~~~~~
make: *** [subdir.mk:34: modbus.o] Error 1
make: Target 'all' not remade because of errors.

Solution

  • The right variant of the "outer" modbus.c is

    #include <stdio.h>
    #include <stdarg.h>
    #include <syslog.h> // I could include it earlier than the "inner" modbus.c will do...
    
    void syslog_printf(int level, char* format, ...)
    {
      va_list params;
      va_start(params, format);
      vprintf(format, params);
      printf("\n");
      va_end(params);
    }
    
    #define syslog(...) syslog_printf(__VA_ARGS__)
    
    #include <modbus/modbus.c>
    

    Thanks to all of you, commented my question.