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

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.

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.

Related

Undefined reference (but nm says the function exists) [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 5 years ago.
I have a library from a camera manufacturer, and in the demo code provided, a function called metadata_init() works fine, in my code though, I get an undefined reference error.
The make output from the demo code:
/opt/linaro-multilib-2013.09-gcc4.8/bin/arm-linux-gnueabihf-gcc -lm -g -L"/home/aro/Downloads" -o hicore demoS2.c -lpthread -lyuvlib -lrt
and thats it, the build succeeds fine, I will have a working ./hicore application in there.
My project is a little bit more complicated, and I compile using eclipse.
The console output is:
11:33:23 **** Build of configuration Camera-R4-Debug for project Camera ****
make all
Building file: ../src/Camera.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Camera.d" -MT"src/Camera.o" -o "src/Camera.o" "../src/Camera.cpp"
../src/Camera.cpp: In function ‘int metadata_construct_http_message(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*)’:
../src/Camera.cpp:379:24: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
"</svg>\r\n";
^
../src/Camera.cpp: In function ‘int main()’:
../src/Camera.cpp:426:97: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
(void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
^
Finished building: ../src/Camera.cpp
Building file: ../src/CameraFrameGrabber.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraFrameGrabber.d" -MT"src/CameraFrameGrabber.o" -o "src/CameraFrameGrabber.o" "../src/CameraFrameGrabber.cpp"
Finished building: ../src/CameraFrameGrabber.cpp
Building file: ../src/CameraLogger.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraLogger.d" -MT"src/CameraLogger.o" -o "src/CameraLogger.o" "../src/CameraLogger.cpp"
Finished building: ../src/CameraLogger.cpp
Building file: ../src/CameraParameter.cpp
Invoking: Cross G++ Compiler
arm-linux-gnueabihf-g++ -I/opt/Camerasdk/R4/include -I"/home/aro/cameraBuilds/Wrapper/include" -I"/opt/ExternalLibraries/curl/Camera/R4/include" -I"/opt/ExternalLibraries/libxml2/Camera/R4/include" -I"/opt/ExternalLibraries/OpenCV24/Camera/R4/include" -I"/home/aro/cameraBuilds/Camera" -I/opt/InternalLibraries/include -I/opt/InternalLibraries/include_linux -I/opt/InternalLibraries/ipslib/include -I/opt/InternalLibraries/ipsstream/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/CameraParameter.d" -MT"src/CameraParameter.o" -o "src/CameraParameter.o" "../src/CameraParameter.cpp"
Finished building: ../src/CameraParameter.cpp
Building target: Camera
Invoking: Cross G++ Linker
arm-linux-gnueabihf-g++ -L/opt/Camerasdk/R4/lib -L"/cameraBuilds/Wrapper/Camera-R4-Debug" -L"/opt/ExternalLibraries/curl/Camera/R4/lib" -L"/opt/ExternalLibraries/libxml2/Camera/R4/lib" -L"/opt/ExternalLibraries/OpenCV24/Camera/R4/lib" -L"/opt/DetectionModules/Camera/R4/lib" -L/home/aro/Downloads -o "Camera" ./src/Camera.o ./src/CameraFrameGrabber.o ./src/CameraLogger.o ./src/CameraParameter.o -lWrapper -lxml2 -lopencv_highgui -lopencv_imgproc -lopencv_core -lpthread -lyuvlib -lrt -llibjasper -llibjpeg -llibpng -llibtiff -lzlib -lcurl
./src/Camera.o: In function `main':
/home/aro/cameraBuilds/Camera/Camera-R4-Debug/../src/Camera.cpp:426: undefined reference to `metadata_init(char*, int, int (*)(char*, METADATA_HTTP_MESSAGE_TYPE, void*, int*))'
collect2: error: ld returned 1 exit status
make: *** [Camera] Error 1
11:33:24 Build Finished (took 1s.212ms)
The code itself is the very same thing, I just copied it over.
Metadata.h:
#ifndef _HIK_METADATA_H_
#define _HIK_METADATA_H_
const int max_http_body_len = 100 * 1024;
typedef enum
{
CMD_ADD_TYPE = 1,
CMD_OTHER,
} METADATA_CTRL_TYPE;
typedef struct
{
int length;
int fd;
METADATA_CTRL_TYPE cmd_type;
} METADATA_HEADER;
typedef struct
{
char option[128];
int share_socket;
} METADATA_ADD_CFG;
typedef enum
{
HTTP_HEADER_TYPE = 1,
HTTP_BODY_TYPE,
} METADATA_HTTP_MESSAGE_TYPE;
typedef struct
{
char boundary[64];
char http_content_type[64];
char multipart_content_type[64];
} METADATA_MULTIPART_TYPE;
typedef int (*p_metadata_construct_http_msg_callback_f)(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len);
int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);
#endif
In Camera.cpp:
#include "Metadata.h"
int metadata_construct_http_message(char *p_option, METADATA_HTTP_MESSAGE_TYPE cmd_type, void *p_data, int *p_data_len)
{
// Removed for SO
return 0;
}
int main()
{
(void)metadata_init("stream.cgi", strlen("stream.cgi"), metadata_construct_http_message);
...
}
What causes this, and how can I debug this issue to narrow down how to fix it?
It think the problem could be that metadata_init is a C function, but you use it from a C++ code.
In case of this, extern "C" must be used in the header file, like this:
#ifdef __cplusplus
extern "C" {
#endif
// embed the whole contents of the header file here, I just put the function here for brevity
int metadata_init(char *p_metadata_url, int url_len, p_metadata_construct_http_msg_callback_f p_callback_f);
#ifdef __cplusplus
}
#endif
It is because of name mangling rules. Names are mangled differently in C and C++. With extern "C", you tell the compiler that the names inside should be used with "C" mangling.
C++ has to do a complex name mangling compared to C, because it has to embed almost all signature information to a name (all parameters type), while in C, the mangled name usually is the same as the function name, or there is a _ prepended.

Multiple definition of function, why isn't the guard catching this?

So I wrote a small set of logging functions in the file cerus.h. The contents of that file can be seen below. It is being included in main.cpp, model.cpp, engine.cpp and camera.cpp. As can be seen, I have include guards so I'm not sure why I'm getting this error:
Output of $ make
jed#ArchPC:~/glPlayground$ make
g++ -std=c++11 -c model.cpp -o bin/model.o
g++ -std=c++11 -c tiny_obj_loader.cc -o bin/tinyobj.o
g++ -std=c++11 -c camera.cpp -o bin/camera.o
g++ -g -std=c++11 -o main bin/main.o bin/engine.o bin/tinyobj.o bin/model.o bin/camera.o -lGL -lGLU -lglut -lSOIL -lGLEW -lglfw
bin/engine.o: In function `LOG(char const*)':
engine.cpp:(.text+0x0): multiple definition of `LOG(char const*)'
bin/main.o:main.cpp:(.text+0x0): first defined here
bin/engine.o: In function `LOGERR(char const*)':
engine.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)'
bin/main.o:main.cpp:(.text+0x3d): first defined here
bin/model.o: In function `LOG(char const*)':
model.cpp:(.text+0x0): multiple definition of `LOG(char const*)'
bin/main.o:main.cpp:(.text+0x0): first defined here
bin/model.o: In function `LOGERR(char const*)':
model.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)'
bin/main.o:main.cpp:(.text+0x3d): first defined here
bin/camera.o: In function `LOG(char const*)':
camera.cpp:(.text+0x0): multiple definition of `LOG(char const*)'
bin/main.o:main.cpp:(.text+0x0): first defined here
bin/camera.o: In function `LOGERR(char const*)':
camera.cpp:(.text+0x3d): multiple definition of `LOGERR(char const*)'
bin/main.o:main.cpp:(.text+0x3d): first defined here
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'main' failed
make: *** [main] Error 1
cerus.h
#ifndef CERUS_H
#define CERUS_H
#include <iostream>
//Need to add Windows and Mac Includes here
// Linux Include Statements
void LOG(const char* str){
std::cout << "[INFO]" << str << "\n";
}
void LOGERR(const char* str){
std::cout << "[ERROR]" << str << "\n";
}
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <GLFW/glfw3.h>
#endif
Makefile
all: main
main: bin/main.o bin/engine.o bin/model.o bin/tinyobj.o bin/camera.o cerus.h
g++ -g -std=c++11 -o main bin/main.o bin/engine.o bin/tinyobj.o bin/model.o bin/camera.o -lGL -lGLU -lglut -lSOIL -lGLEW -lglfw
bin/main.o: main.cpp cerus.h
g++ -std=c++11 -c main.cpp -o bin/main.o
bin/engine.o: engine.cpp engine.h cerus.h
g++ -std=c++11 -c engine.cpp -o bin/engine.o
bin/tinyobj.o: tiny_obj_loader.cc tiny_obj_loader.h cerus.h
g++ -std=c++11 -c tiny_obj_loader.cc -o bin/tinyobj.o
bin/model.o: model.cpp model.h cerus.h
g++ -std=c++11 -c model.cpp -o bin/model.o
bin/camera.o: camera.cpp camera.h cerus.h
g++ -std=c++11 -c camera.cpp -o bin/camera.o
clean:
rm -f bin/*.o main
If someone could explain to me why my guard isn't catching this, I would greatly appreciate the help.
EDIT: Fixed this issue by adding a file called cerus.cpp and defining my logging functions there instead of in cerus.h
This type of guard is to avoid things from being declared or defined in same translation unit.
It won't have any effect for multiple definition in different translation units (i.e. multiple source files).
In this case, you should move the definitions of functions LOG and LOGERR to another .cpp file, and put declarations of the functions in the header file.
Guards did nothing wrong, they just protect your declarations/inlines/templates.
It's the definitions that are real issue. If you have inline functions in your cpp, put them in header, same for templates. Do not include cpp files. Can't see much of your code but that is most of the cases.

Why's the linker not finding just a single (present) method in a library?

So I recently started using a library called libgambatte (github), everything was going just fine until the linker started complaining about an undefined reference for a method. What's really strange is that it find every other method for that object! Arguments' types are correct (I've copied them from the working program included with the library).
Here's the code:
Make output:
g++ -Wall -Wextra -g3 -I../libgambatte/include -I../common -c -o test.o test.cpp
g++ test.o -L../libgambatte -lgambatte -lz -o test
test.o: In function `main':
<REDACTED>/src/test.cpp:12: undefined reference to `gambatte::GB::runFor(unsigned long*, long, unsigned long*, unsigned long&)'
collect2: error: ld returned 1 exit status
Makefile:9: recipe for target 'test' failed
make: *** [test] Error 1
Makefile:
CPPFLAGS=-Wall -Wextra -g3 -I../libgambatte/include -I../common
LDFLAGS=-L../libgambatte -lgambatte -lz
.PHONY=all clean
all: test
test: test.o
$(CXX) test.o $(LDFLAGS) -o test
clean:
rm -f test.o test
Output of nm ../libgambatte/libgambatte.a | grep runFor:
0000000000007a20 T _ZN8gambatte3CPU6runForEm
0000000000000190 T _ZN8gambatte2GB6runForEPjlS1_Rm
U _ZN8gambatte3CPU6runForEm
Source for test.cpp:
#include <gambatte.h>
using namespace std;
int main(void){
gambatte::uint_least32_t audiobuf[1234];
gambatte::uint_least32_t framebuf[1234];
size_t samples = 1234;
unsigned gb_width = 160;
gambatte::GB gb;
gb.runFor(framebuf, gb_width, audiobuf, samples);
return 0;
}
runFor method declaration:
std::ptrdiff_t runFor(gambatte::uint_least32_t *videoBuf, std::ptrdiff_t pitch,
gambatte::uint_least32_t *audioBuf, std::size_t &samples);
Definition for gambatte::uint_least32_t:
#ifdef HAVE_CSTDINT
#include <cstdint>
namespace gambatte {
using std::uint_least32_t;
using std::uint_least16_t;
}
#elif defined(HAVE_STDINT_H)
#include <stdint.h>
namespace gambatte {
using ::uint_least32_t;
using ::uint_least16_t;
}
#else
namespace gambatte {
#ifdef CHAR_LEAST_32
typedef unsigned char uint_least32_t;
#elif defined(SHORT_LEAST_32)
typedef unsigned short uint_least32_t;
#elif defined(INT_LEAST_32)
typedef unsigned uint_least32_t;
#else
typedef unsigned long uint_least32_t;
#endif
#ifdef CHAR_LEAST_16
typedef unsigned char uint_least16_t;
#else
typedef unsigned short uint_least16_t;
#endif
}
#endif
Sorry for the long code.
I've found the problem: as you can see in the definition of gambatte::uint_least32_t, the type definition is based on the presence of certain constants. My problem manifested because the library (and the included program) are compiled with -DHAVE_STDINT_H and therefore define gambatte::uint_least32_t as a different type, causing the linker to not find the correct signature; compiling with -DHAVE_STDINT_H solved the problem for me.

undefined reference to `GDALAllRegister'

I have problem with gdal.Iam currently working on Ubuntu and the IDE is Eclipse CDT i have given my code below and i have also installed the gdal package but it gives build errors
#include <stdio.h>
#include<unistd.h>
#include "gdal/gdal_priv.h"
#include "gdal/cpl_conv.h"
#include<gdal/gdal.h>
int main(int argc,char* argv[])
{
void *hLib_Comm;
char exePath[800];
if(getcwd(exePath,sizeof(exePath))== NULL) {
printf("Exe path access error\n");
}
GDALDataset *poDataset;
GDALAllRegister();
poDataset = (GDALDataset *) GDALOpen( exePath, GA_ReadOnly );
if( poDataset == NULL )
{
printf("Exe path access error\n");;
}
}
///////////Bulid result///////////////////////////////////
14:51:47 **** Build of configuration Debug for project ReadFrmSoFile ****
make all
Building file: ../main.cpp
Invoking: GCC C++ Compiler
g++ -lm -I/usr/include/gdal -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp"
../main.cpp: In function ‘int main(int, char**)’:
../main.cpp:17:8: warning: unused variable ‘hLib_Comm’ [-Wunused-variable]
Finished building: ../main.cpp
Building target: ReadFrmSoFile
Invoking: GCC C++ Linker
g++ -lm -L/usr/lib -L/usr/lib/ogdi -o "ReadFrmSoFile" ./main.o -lgdal
./main.o: In function `main':
/media/Local Disk/workspace2/ReadFrmSoFile/Debug/../main.cpp:24: undefined reference to `GDALAllRegister'
/media/Local Disk/workspace2/ReadFrmSoFile/Debug/../main.cpp:26: undefined reference to `GDALOpen'
collect2: ld returned 1 exit status
make: *** [ReadFrmSoFile] Error 1
//////////////////////////////////////////////
Please Help Me......

undefined reference to 'main' when compiling

Possible dup of this or this, but even after trying to dig through the answers for a while, I couldn't resolve this.
While trying to compile following makefile,
all: test
test: constants.h Point.h Point.cpp line_t.h line_t.cpp drawing_t.h drawing_t.cpp clipper_t.h clipper_t.cpp main.cpp
g++ -o test Point.cpp line_t.cpp drawing_t.cpp clipper_t.cpp main.cpp -lglut
I get an error:
g++ -o test Point.cpp line_t.cpp drawing_t.cpp clipper_t.cpp main.cpp
-lglut /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function _start': (.text+0x18): undefined reference tomain'
collect2: ld returned 1 exit status make: *** [test] Error 1
I am new at Makefile. I guess, I am missing something too obvious.
Apparently none your files define a function with the signature
int main();
or
int main(int argc, char *argv[]);