eclipse CDT g++ unable to link minimodem c code in c++ project - c++

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;

Related

Building gcc plugin for windows on linux

Try to build the following gcc plugin on linux for windows with mingw cross compiler. The plugins are from the built avr compiler also for windows. Adapted the following plugin https://github.com/jcmvbkbc/avr-flash-vtbl.
#include <gcc-plugin.h>
#include <cp/cp-tree.h>
#ifdef _WIN32
__declspec(dllexport)
#endif
int plugin_is_GPL_compatible = 1;
void fn(void *gcc_data, void *user_data)
{
TYPE_ADDR_SPACE (TREE_TYPE (vtbl_type_node)) = 1;
TYPE_ADDR_SPACE (TREE_TYPE (vtbl_ptr_type_node)) = 1;
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int plugin_init (struct plugin_name_args *plugin_info,
struct plugin_gcc_version *version)
{
register_callback("", PLUGIN_START_UNIT, fn, NULL);
return 0;
}
Output during compile and linking:
i686-w64-mingw32-g++ -shared -I/home/andreas/omgwtfbbq/win64/bin/../lib/gcc/avr/9.2.0/plugin/include -Wl,--export-all-symbols /home/andreas/
omgwtfbbq/win64/bin/../lib/gcc/avr/9.2.0/plugin/cc1plus.exe.a avr-flash-vtbl.c -o avr-flash-vtbl.so -I./
/usr/bin/i686-w64-mingw32-ld: /tmp/cc28ZVde.o:avr-flash-vtbl.c:(.text+0x4): undefined reference to `cp_global_trees'
/usr/bin/i686-w64-mingw32-ld: /tmp/cc28ZVde.o:avr-flash-vtbl.c:(.text+0x10): undefined reference to `cp_global_trees'
/usr/bin/i686-w64-mingw32-ld: /tmp/cc28ZVde.o:avr-flash-vtbl.c:(.text+0x44): undefined reference to `register_callback'
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: avr-flash-vtbl.so] Fehler 1
The compiler flags are adapted from https://gcc.gnu.org/onlinedocs/gccint/Plugins-building.html. Has anybody already faced such a issue?
Problem is solved. Changed host compiler from i686-w64-mingw32-g++ to x86_64-w64-mingw32-g++ and changed the order of the options. /home/andreas/omgwtfbbq/win64/bin/../lib/gcc/avr/9.2.0/plugin/cc1plus.exe.a must go after avr-flash-vtbl.c.

C++ Cygwin Undefined reference to constructor

While trying to build my small test I encounter an error I don't understand why it shouldn't work. I'm using Eclipse and Cygwin.
The Header and the Source files are separated into different Folders and I put them also into the Cygwin include folders.
Console log
16:05:41 **** Incremental Build of configuration Debug for project Testarea ****
make all
Building target: Testarea.exe
Invoking: Cygwin C++ Linker
g++ -o "Testarea.exe" ./Source/lint.o ./Source/tester.o
./Source/tester.o: In function `main':
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4: undefined reference to `lint::lint()'
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4:(.text+0x20): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `lint::lint()'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: Testarea.exe] Error 1
16:05:41 Build Finished (took 348ms)
tester.cpp
#include <iostream>
#include <lint.h>
int main(){
lint* a = new lint();
std::cout << "hallo";
return 0;
}
lint.cpp
class lint{
private:
int* a;
public:
lint(){
a = new int();
};
lint(int b){
a = new int(b);
};
lint(lint& b){
a = new int(b.value());
};
int value(){
return *a;
};
};
lint.h
#ifndef HEADER_LINT_H_
#define HEADER_LINT_H_
class lint{
public:
lint();
lint(int b);
lint(lint& b);
int value();
};
#endif
Your problem is that you've got 2 classes there. One called lint and the other called lint but only available in the lint.cpp file.
Implementations are done:
#include "lint.h"
lint::lint() {}
and so forth.

Creating a Makefile for C++ and C source code [duplicate]

I'm getting an undefined referenced error, not knowing the reason why.
So I have 2 files which makes a static lib : keyboard_input.c, keyboard_input.h
Here's the content of the .h file:
#ifndef __MOD_KBINPUT__
#define __MOD_KBINPUT__
int kbInit();
int kbWait();
int kbTest();
#endif
And the CMakeLists.txt file looks like this:
FILE(
GLOB_RECURSE
sources
*.c
)
INCLUDE_DIRECTORIES("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
ADD_LIBRARY(keyboardReader ${sources})
Compiling this lib gives some warnings:
src/utils/kbreader/keyboard_input.c: In function ‘kbInit’:
src/utils/kbreader/keyboard_input.c:13:14: warning: assignment from incompatible pointer type [enabled by default]
src/utils/kbreader/keyboard_input.c: In function ‘kbWait’:
src/utils/kbreader/keyboard_input.c:21:55: warning: passing argument 4 of ‘fread’ from incompatible pointer type [enabled by default]
/usr/include/stdio.h:708:15: note: expected ‘struct FILE * __restrict__’ but argument is of type ‘struct FILE *’
Now, for my main executable (main.cpp):
#include <keyboard_input.h>
int main()
{
kbTest();
return 0;
}
Processed by the following CMakeLists.txt file:
include_directories("${PROJECT_SOURCE_DIR}/include/utils/kbreader")
file(
GLOB_RECURSE
srcs
*.cpp
)
add_executable(
PEM
${srcs}
)
target_link_libraries(PEM keyboardReader)
Ends up getting that error:
CMakeFiles/PEM.dir/main.cpp.o: In function `main':
main.cpp:(.text+0xb): undefined reference to `kbTest()'
collect2: ld returned 1 exit status
make[2]: *** [src/PEM/main2/PEM] Error 1
make[1]: *** [src/PEM/main2/CMakeFiles/PEM.dir/all] Error 2
The libkeyboardReader.a is created, and the kbTest() function doesn't do anything except
{return 0; }
If I set the definition of kbTest() in the header file, it works.
But there's something i don't get, when i type: make keyboardReader here is the output:
[ 73%] Building C object src/utils/kbreader/CMakeFiles/KeyboardReader.dir/keyboard_input.c.o
[Warning explained above]
Linking C static library ../../../lib/libKeyboardReader.a
Is there something wrong? Does the note error message makes my lib omit the keyboard_input.c file?
You're mixing C and C++ files. To make that work, you just have to tell the C++ compiler that it's calling a C function, by changing the header file like so:
#ifndef MOD_KBINPUT
#define MOD_KBINPUT
/* note I also fixed the macro so you aren't using a system-reserved name */
#if __cplusplus
/* this is the important part */
extern "C" {
#endif
int kbInit();
int kbWait();
int kbTest();
#if __cplusplus
}
#endif
#endif
Otherwise the C++ compiler assumes the function will be given a C++ internal name (which encodes all the type information in the signature, this is what lets the linker distinguish between overloaded functions) and then the linker doesn't find it.

C++ Eclipse g++: error: ./Matrix.o: No such file or directory make: *** [MatrixSample] Error 1

I am trying to make a program that can manipulate matrixes and I am getting that error in Eclipse on Ubuntu 14.04. I searched on google and on stack over flow, but I did not find anything that I could understand and I am stuck. May anybody help me out with this please?
The complete error message:
14:29:27 **** Incremental Build of configuration Debug for project MatrixSample ****
make all
Building file: ../Matrix.inl
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Matrix.d" -MT"Matrix.d" -o "Matrix.o" "../Matrix.inl"
g++: warning: ../Matrix.inl: linker input file unused because linking not done
Finished building: ../Matrix.inl
Building target: MatrixSample
Invoking: GCC C++ Linker
g++ -o "MatrixSample" ./Main.o ./Matrix.o
g++: error: ./Matrix.o: No such file or directory
make: *** [MatrixSample] Error 1
Here is the code:
/*
* Main.cpp
*/
#include "Matrix.hpp"
int main()
{
Matrix<int> firstMatrix();
return 0;
}
/*
* Matrix.hpp
*
*/
#ifndef MATRIX_H_
#define MATRIX_H_
template <typename T>
class Matrix
{
public:
Matrix();
private:
int nbrRows;
int nbrColumns;
};
#include "Matrix.inl"
#endif /* MATRIX_H_ */
/*
* Matrix.inl
*/
template <typename T>
Matrix<T>::Matrix()
:nbrRows(0), nbrColumns(0){}
I found what the problem was. I added .inl files as C++ Source file instead of adding it to C++ Header file. Now it's working fine.

undefined reference to `__imp_WSACleanup'

This is my first program with winsock. As you can see, I've #include <winsock2.h> and linked ws2_32.dll, but the code still doesn't compile:
#include<winsock2.h>
#pragma comment(lib, "ws2_32")
class CInitSock{
public:
CInitSock(BYTE minorVer=2,BYTE majorVer=2){
//initialize WS2_32.dll
WSADATA wsaData;
WORD sockVersion = MAKEWORD(minorVer,majorVer);
if(::WSAStartup(sockVersion,&wsaData)!=0){
exit(0);
}
}
//release winSock libary
~CInitSock(){
::WSACleanup();
}
};
#include "CInitSock.h"
#include<stdio.h>
CInitSock initSock;
int main(void){
char szHost[256];
::gethostname(szHost,256);
hostent *phost = ::gethostbyname(szHost);
in_addr addr;
for(int i = 0;;i++){
char *p = phost->h_addr_list[i];
if(p==NULL){
break;
}
memcpy(&addr.S_un.S_addr,p,phost->h_length);
char *szIp = ::inet_ntoa(addr);
printf("%s \n",szIp);
}
}
This is the error:
mingw32-make.exe -f "D:\project\c_program\Makefile.win" all
g++.exe GetAllIPs.o -o win_socket.exe -L"D:/tools/develepment/Dev-Cpp/MinGW64/x86_64- w64-mingw32/lib" -L"D:/tools/develepment/Dev-Cpp/MinGW64/lib32" -static-libgcc -mwindows -g3
GetAllIPs.o: In function `main':
D:\project\c_program/GetAllIPs.cpp:6: undefined reference to `__imp_gethostname'
D:\project\c_program/GetAllIPs.cpp:7: undefined reference to `__imp_gethostbyname'
D:\project\c_program/GetAllIPs.cpp:15: undefined reference to `__imp_inet_ntoa'
GetAllIPs.o: In function `CInitSock::CInitSock(unsigned char, unsigned char)':
D:\project\c_program/CInitSock.h:10: undefined reference to `__imp_WSAStartup'
GetAllIPs.o: In function `CInitSock::~CInitSock()':
D:\project\c_program/CInitSock.h:16: undefined reference to `__imp_WSACleanup'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe: *** [win_socket.exe] Error 1
Now I'm totally confused...
The pragma you use only works for the Visual C++ Compiler and will be ignored by the gcc
#pragma comment(lib, "ws2_32")
you have to add the ws2_32.lib it manually in the makefile.
like:
-L"ws2_32"
(I guess it was without the ".lib" at the end)
at the end of the g++ line. You have of course add the full path which I have not by hand at the moment.
I met the same problem with you. I solved it by adding a command -lwsock32.
you can add the command according follow steps:
tools
compiler options
choose general
click add the following commands when calling the compilers
then you can add the above command -lwsock32.
add
-lwsock32
to your command-line instead of #pragma when compiling with MinGW
g++ src/main.cpp -o release/myApp.exe -lwsock32
In DevC++, navigate to Project >> Project Options (or via usually ctrl+h); then in the "Parameters" tab there is a button "Add Library or Object" and then add libws2_32.a.
For codeblocks under wnidows : go to
Project -> Build Options -> Linker settings (make sure the project is selected on the left, not a build target) and add (type in) in the left list the library "ws2_32"