1

I am trying to compile a c++ project in Eclipse CDT with third party C project (minimodem https://github.com/kamalmostafa/minimodem), Currently my project simply contains a few cpp files and folder inside the source folder with C files. I have included c headers the main.cpp c files using

extern "C"
{

    #include "minimodem/simpleaudio.h"
    #include "minimodem/fsk.h"
    #include "minimodem/databits.h"
}

the code compiles but breaks at linking with below error

**** Build of configuration Debug for project Sample_Test ****

make all 
Building target: Sample_Test
Invoking: GCC C++ Linker
g++ -L/usr/include -o "Sample_Test"  ./minimodem/baudot.o ./minimodem/databits_ascii.o ./minimodem/databits_baudot.o ./minimodem/databits_binary.o ./minimodem/databits_callerid.o ./minimodem/databits_uic.o ./minimodem/fsk.o ./minimodem/simple-tone-generator.o ./minimodem/simpleaudio-alsa.o ./minimodem/simpleaudio-benchmark.o ./minimodem/simpleaudio-pulse.o ./minimodem/simpleaudio-sndfile.o ./minimodem/simpleaudio.o ./minimodem/uic_codes.o  ./aes256.o ./main.o   -lfftw3f -lasound -lpulse-simple -lpulse -lsndfile
./minimodem/simpleaudio.o: In function `simpleaudio_open_stream':
/home/user1/workspace/Sample_Test/Debug/../minimodem/simpleaudio.c:88: undefined reference to `simpleaudio_backend_pulseaudio'
makefile:45: recipe for target 'Sample_Test' failed
/home/user1/workspace/Sample_Test/Debug/../minimodem/simpleaudio.c:99: undefined reference to `simpleaudio_backend_alsa'
/home/user1/workspace/Sample_Test/Debug/../minimodem/simpleaudio.c:105: undefined reference to `simpleaudio_backend_pulseaudio'
collect2: error: ld returned 1 exit status
make: *** [Sample_Test] Error 1

**** Build Finished ****

inside simpleaudio.c the function simpleaudio_open_stream (third party C code)

..
#include "simpleaudio.h"
#include "simpleaudio_internal.h"
..
simpleaudio_open_stream(
...
simpleaudio *sa = calloc(1, sizeof(simpleaudio));
...
#if USE_PULSEAUDIO
        sa->backend = &simpleaudio_backend_pulseaudio;
#elif USE_ALSA
..

#if USE_ALSA
    case SA_BACKEND_ALSA:
        sa->backend = &simpleaudio_backend_alsa;
        break;
#endif

#if USE_PULSEAUDIO
    case SA_BACKEND_PULSEAUDIO:
        sa->backend = &simpleaudio_backend_pulseaudio;
        break;
#endif
...
)

the structs linker is unable to find reside in simpleaudio_internal.h

extern const struct simpleaudio_backend simpleaudio_backend_benchmark;
extern const struct simpleaudio_backend simpleaudio_backend_sndfile;
extern const struct simpleaudio_backend simpleaudio_backend_alsa;
extern const struct simpleaudio_backend simpleaudio_backend_pulseaudio;
user482963
  • 342
  • 6
  • 17
  • The third party library (which is *probably* [this one](https://github.com/kamalmostafa/minimodem), you should specify in your question) uses conditional compilation for those backend variables. Do you build all the files of the library with the same flags and configuration? You *did* configure is properly? Like executing its `configure` script (possibly after generating it)? – Some programmer dude Apr 24 '18 at 07:25
  • no I simply included the source in my eclipse project with all required system libraries added to the linker – user482963 Apr 24 '18 at 08:10
  • Then the `USE_ALSA` and other macros may not be properly set for all source files. – Some programmer dude Apr 24 '18 at 08:22
  • I see, but I have added both USE_ALSA and USE_PULSEAUDIO to GCC C++ Compiler Settings – user482963 Apr 24 '18 at 08:24
  • 1
    And if you look at the build-log for the minimodem source file compilations, those macros are passed to all the minimodem source files? – Some programmer dude Apr 24 '18 at 08:25
  • Apparently No, they are only being include for cpp files not c files ------------------- Building file: ../minimodem/simpleaudio.c Invoking: GCC C Compiler gcc -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"minimodem/simpleaudio.d" -MT"minimodem/simpleaudio.d" -o "minimodem/simpleaudio.o" "../minimodem/simpleaudio.c" Finished building: ../minimodem/simpleaudio.c g++ -DUSE_PULSEAUDIO -DUSE_ALSA -UUSE_PULSEAUDIO -UUSE_ALSA -I"/home/user1/workspace/Sample_Test/minimodem" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.cpp" – user482963 Apr 24 '18 at 08:29
  • 1
    thanks I think I got it, Open Project Properties--> C/C++ General-->Paths and Symbols-->Symbols-->GNU C-->Add then Name:USE_PULSEAUDIO and set Value to 1 check both boxes saying Add to all configurations and Add to all languages--> Ok do the same for USE_ALSA – user482963 Apr 24 '18 at 08:42

0 Answers0