Undefined reference error for one specific library function - c++

I'm writing software to control a bladeRF radio card but I'm running into a strange compiler/linker error that I haven't been able to figure out. My code uses several functions and data structures defined in the library, libbladeRF, but for some reason I can't reference to one specific function.
However, if I modify the call with an improper argument type, g++ will throw an error to let me know that it doesn't conform to the definition, which seems to tell me that the linker is actually able to locate the reference.
What am I missing?
Initial error:
$ g++ bladeRF_test.cpp -o bladeRF_test -lbladeRF
/tmp/ccTWZzdJ.o: In function `enable_xb300()':
bladeRF_test.cpp:(.text+0x36a): undefined reference to `bladerf_xb300_set_amplifier_enable'
Code excerpt:
#include <iostream>
#include <string>
#include <libbladeRF.h>
using namespace std;
...
int set_xb300_pa(bool enable) {
bladerf_xb300_amplifier amp = BLADERF_XB300_AMP_PA;
if ( bladerf_xb300_set_amplifier_enable(dev, amp, enable) ) {
// Print error message
return -1;
} else {
// Print success message
return 0;
}
}
...
Function arguments changed from (dev, amp, enable) to (&dev, amp, enable):
$ g++ blade_hello.cpp -o blade_hello -lbladeRF
blade_hello.cpp: In function ‘int set_xb300_pa()’:
blade_hello.cpp:62:59: error: cannot convert ‘bladerf**’ to ‘bladerf*’ for argument ‘1’ to ‘int bladerf_xb300_set_amplifier_enable(bladerf*, bladerf_xb300_amplifier, bool)
^
In file included from blade_hello.cpp:4:0:
/usr/local/include/libbladeRF.h:2226:15: note: declared here
int CALL_CONV bladerf_xb300_set_amplifier_enable(struct bladerf *dev,
^

Related

Get the following errors when I compile with G++

I am a bit new to programming as you can probably tell from my prior question(s). I was wondering if anyone could help me with this recent problem I've had. I am trying to compile a script main.cpp using g++ but I get the following errors:
Donny#Donny-PC /cygdrive/c/Users/Donny/Desktop/equation/equations/equations
$ g++ main.cpp -o don.exe
main.cpp:3:11: error: ‘::main’ must return ‘int’
void main(){
^
main.cpp: In function ‘int main()’:
main.cpp:36:22: error: ‘pow’ was not declared in this scope
float n=pow(10.0,9.0);
^
main.cpp:43:27: error: ‘sin’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:43:44: error: ‘tan’ was not declared in this scope
float R56=(lb1/sin(theta1)) * ((tan(theta1))-theta1) + (lb2/sin(theta1)) * ((tan(theta1))-theta1) +
^
main.cpp:48:40: error: ‘cos’ was not declared in this scope
d*((pow(tan(theta1),2))/cos(theta1)) +
^
The weird thing is that this code works when compiled with microsoft visual studio 2010 C++. Any help would be greatly appreciated!
EDIT:
So, the fixed a lot of the errors shown above, but I am still having a little difficulty fixing the void main error. Here is how my code looks:
#include<iostream>
#include<cmath>
using namespace std;
void main(){
float r, i, f, beta, alpha;
cout<<"Enter value of R : ";............
Any help or examples would be greatly appreciated.
The first error should be self-explanatory. The standard says that the main function must return int but you have declared it as void. Return 0 from your main function to indicate normal termination. The Microsoft compiler is not as strict on this point.
All your remaining errors can be remedied by using #include <math.h>.

Class method returns bool, but compiler is treating it like a void return

EDIT
Still don't know what the problem is. Seemed allot like I had multiple files, but sure I didn't. Anyway, I renamed my class .h and .cc filed, and renamed the classes themselves. Didn't actually change any code, but now the problem is gone.
I would like to get rid of this question as I cant see it helping anyone in the future. Obviously was a local mistake of mine.
I am calling a method from my main function and using the boolean return in an if statement. The method is declared as a bool return, but the compiler seems to think its a void return. I've tried changing to bool to int and changing the if evaluation, but the compiler still seems to think its a void function. I have no idea what's wrong.
method deceleration inside class definition (as public)
bool check_online();
method definition inside class cc file
bool Icarus::check_online()
{
char ret = 0x00;
//check if bluetooth adapter exists
if(! input)
{
mvwaddstr(feedback,1,1,"COULD NOT OPEN rfcomm0 FOR SERVICE");
wrefresh(feedback);
getch();
return(0);
}
//check if Daedalus is online
if(send_cr())
{
mvwaddstr(feedback,1,1,"Daedalus is Online ");
wrefresh(feedback);
return(1);
}
else
{
mvwaddstr(feedback,1,1,"Daedalus is Offline ");
wrefresh(feedback);
return(0);
}
}
calling the method in main.
if(driver.check_online() == false)
{
getch();
cleanup();
}
resulting error
$ g++ -g icarus.cc main.cc -o icarus -lncurses -lpthread
main.cc: In function ‘int main()’:
main.cc:34:30: error: invalid operands of types ‘void’ and ‘bool’ to binary ‘operator==’
I understand what the compiler error means, but I dont understand why its being thrown up. The function is clearly a bool return. Any help?

How to create a glfw thread in c++?

I just started using glfw to try and built a game. I'm fairly new to C and C++ but I worked with openGL before for android. I have got all the openGL stuff working and now started to try and make a thread with glfw.
Here is some basic test code. Its similar to whats in the documentation.
#include <GL/glfw.h>
#include <stdio.h>
GLFWthread thread;
void GLFWCALL testThread()
{
printf("hello\n");
}
int main()
{
printf("test\n");
glfwInit();
thread = glfwCreateThread(testThread, NULL);
glfwWaitThread(thread, GLFW_WAIT);
glfwTerminate();
return 1;
}
This will compile fine in gcc and work as exspected.
$ gcc -o glthread glthread.c -lglfw
$ ./glthread
test
hello
The problem is i want to take advantage of c++ features like classes is my game. When I compile in g++ i get this...
$ g++ -o glthread glthread.c -lglfw
glthread.c: In function ‘int main()’:
glthread.c:18: error: invalid conversion from ‘void (*)()’ to ‘void (*)(void*)’
glthread.c:18: error: initializing argument 1 of ‘GLFWthread glfwCreateThread(void (*)(void*), void*)’
When i put it in a class the crucial error changes to this.
error: argument of type ‘void (Renderer::)()’ does not match ‘void (*)(void*)’
What I basically want to know is, is it possible to create threads using glfw in c++ and if so how?
My main PC for working on this is an arch linux machine. I can't give the versions of my compilers right now. If it will help I can get them later.
void GLFWCALL testThread()
{
printf("hello\n");
}
Should receive one argument of type void* and you cannot use class-functions here, since signature of pointer to class function is Ret (Class::*)(args), not void (*)(void*). If you want use pointers to class-members with threads - you should use more C++ style libraries (boost::thread, or something like it, or write your own wrapper).
Your example works in C, since in C empty brackets (i.e. () ) means - any number of parameters of any types, but in C++ () means, that function shouln't receive parameters at all.

Linker Error With Static Library

I have successufully compiled my spellcheck program and libspellcheck library. Now my problem is allowing other developers to use my libspellcheck. I created a simple test program:
#include <spellcheck.h>
#include <iostream>
using namespace std;
int main(void)
{
bool spell_result = check_spelling("english.dict", "abed");
if(spell_result == true)
{
cout << "your dictionary / libspellcheck works!" << endl;
}
else
{
cout << "problem with your dictionary / libspellcheck" << endl;
}
return 0;
}
If everything is functioning fine, the program will output:
your dictionary / libspellcheck works
However, this program won't even compile. I used:
g++ -lspellcheck -o test test.cpp
And it did not work. I believe this is a problem with the header file, as the compiler gives me this:
test.cpp: In function ‘int main()’:
test.cpp:9:59: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
test.cpp:9:59: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
/tmp/ccIz3ivT.o: In function `main':
test.cpp:(.text+0x19): undefined reference to `check_spelling(char*, char*)'
collect2: ld returned 1 exit status
The only problem is, spellcheck.h is located in /usr/include, which is where I believe it should be. My question is, how do I fix this error, and was a it a problem with my header file or was it a problem with my libspellcheck. If you need to look at additional code, I will gladly provide it, as spellcheck and libspellcheck are licensed under the GPL.
Assuming your check_spelling declaration in header is correct, try this:
g++ -o test test.cpp -lspellcheck
(-l should be after the objects depending on the libraries in the command line). As of the header, it is indeed found and used, or you would get an error from the compiler, not the linker.

'class X' has no member 'Y'

This error is inexplicably occurring. Here is the code and output:
timer.cpp:
#include "timer.h"
#include "SDL.h"
#include "SDL_timer.h"
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
timer.h:
#include "SDL.h"
#include "SDL_timer.h"
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
Compiler errors:
make
g++ -Wall -I/usr/local/include/SDL -c timer.cpp
timer.cpp: In member function ‘void cTimer::recordCurrentTime()’:
timer.cpp:6: error: ‘class cTimer’ has no member named ‘previous_t’
timer.cpp:6: error: ‘class cTimer’ has no member named ‘current_t’
timer.cpp:7: error: ‘class cTimer’ has no member named ‘current_t’
make: *** [timer.o] Error 1
Compiler errors after removing the #include "timer.h"
g++ -Wall -I/usr/local/include/SDL -c ctimer.cpp
ctimer.cpp:4: error: ‘cTimer’ has not been declared
ctimer.cpp: In function ‘void recordCurrentTime()’:
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:5: error: invalid use of ‘this’ in non-member function
ctimer.cpp:6: error: invalid use of ‘this’ in non-member function
make: *** [ctimer.o] Error 1
Works for me. Are you sure you've got the right timer.h? Try this:
cat timer.h
and verify that it's what you think it is. If so, try adding ^__^ at the beginning of your .h file and seeing if you get a syntax error. It should look something like this:
[/tmp]> g++ -Wall -I/tmp/foo -c timer.cpp
In file included from timer.cpp:1:
timer.h:1: error: expected unqualified-id before ‘^’ token
This seems very odd as
class cTimer
{
private:
int previous_t;
int current_t;
float delta_time;
float accumulated_time;
int frame_counter;
public:
void recordCurrentTime();
float getDelta();
void incrementAccumulator();
void decrementAccumulator();
bool isAccumulatorReady();
void incrementFrameCounter();
void resetFrameCounter();
int getFPS();
};
void cTimer::recordCurrentTime()
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Compiles OK for me.
This suggests that the compiler think cTimer is different from what you've put in your header. So maybe its getting a definition of cTimer from another source file? For this to be the case your "timer.h" would have to not be gettting included correctly. So maybe the wrong timer.h.
A way to check this would be to save the compiler preprocessor output and search that for cTimer.
Another option might be to put a syntax error in your timer.h and make sure the compile fails.
Anyway hope this helps
Some compilers have their own timer.h, this is a name conflict.
Or it is a something else of bizarre bug...
Try renaming timer.h and timer.cpp to something more descriptive like ClassTimer.h and ClassTimer.cpp, maybe the compiler is linking another file named 'timer' since it is a very generic name. Also try this in timer.cpp:
void cTimer::recordCurrentTime(void)
{
this->previous_t = this->current_t;
this->current_t = SDL_GetTicks();
}
Edit: code edited