"collect2: error: ld returned 1 exit status" on Codeblocks (Raspberry Pi 3b/GCC 5.4.0) - codeblocks

Currently I'm learning how to create games (at a low level) at my degree. I'm programming on Ubuntu mate 16.04, Codeblocks 13.12 and this happen:
-------------- Build: Debug in s04 (compiler: GNU GCC Compiler)---------------
g++ -o bin/Debug/s04 obj/Debug/main.o obj/Debug/Pantalla.o
obj/Debug/main.o: file not recognized: File format not recognized
collect2: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
I must create a new window where my "game" will run... (I add code)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "Pantalla.h"
//Ej.1
struct BalaRep
{
int x;
int y;
int vx;
int vy;
};
typedef struct BalaRep * Bala;
//Ej.2
Bala crea_bala ( double x, double y, double vx, double vy )
{
Bala b=malloc(sizeof(struct BalaRep));
b->x = x;
b->y = y;
b->vx = vx;
b->vy = vy;
return b;
}
//Ej.3
void libera_bala( Bala b )
{
free(b);
}
//Ej.4
void mueve_bala( Bala b )
{
b->x = (b->x)+ (b->vx);
b->y = (b->y)+ (b->vy);
}
//Ej.5
void dibuja_bala( Bala b )
{
Pantalla_DibujaRectangulo( b->x, b->y, 7, 7);
}
//Ej.6
/*
double get_x_bala( Bala b )
{
return b->x;
}
*/
//Ej.7
/*
double get_y_bala( Bala b )
{
return b->y;
}
*/
int main( int argc, char *argv[] )
{
Pantalla_Crea("Ejemplo 3", 640,480);
Pantalla_ColorTrazo(255,0,0, 255);
int x = 280;
int y = 425;
int x2 = 200;
int y2 = 100;
int vx2 = 5;
Bala b = NULL;
while ( Pantalla_Activa() )
{
//Crear bala
if (Pantalla_TeclaPulsada(SDL_SCANCODE_SPACE))
{
libera_bala(b);
b=NULL;
b=crea_bala(x,y,0,-10);
}
//Movimiento del rectángulo
if (Pantalla_TeclaPulsada(SDL_SCANCODE_RIGHT))
{
x = x + 5;
}
if (Pantalla_TeclaPulsada(SDL_SCANCODE_LEFT))
{
x = x - 5;
}
/*if (Pantalla_TeclaPulsada(SDL_SCANCODE_UP))
{
y = y - 5;
}
if (Pantalla_TeclaPulsada(SDL_SCANCODE_DOWN))
{
y = y + 5;
}*/
//Bordes no-salir
if (x > 640-80)
{
x = 640 - 80;
}
if (x < 0)
{
x = 0;
}
if (y > 480-40)
{
y = 480 - 40;
}
if (y < 0)
{
y = 0;
}
//Mov enemigo
x2 = x2 + vx2;
//Bordes no-salir enemigo
if (x2 > 640-80)
{
x2 = 640 - 80;
vx2 = vx2 * (-1);
}
if (x2 < 0)
{
x2 = 0;
vx2 = vx2 * (-1);
}
//BALA
if (b!=NULL)
{
mueve_bala(b);
}
if ((b!=NULL) && ((b->y) <= 0))
{
libera_bala(b);
b=NULL;
}
Pantalla_DibujaRellenoFondo( 255,255,255, 255 );
Pantalla_DibujaRectangulo( x, y, 80,40 );
Pantalla_DibujaRectangulo( x2, y2, 80,40 );
if (b!=NULL)
{
dibuja_bala(b);
}
Pantalla_Actualiza();
Pantalla_Espera(40);
}
Pantalla_Libera();
return 0;
}
There is a file that teachers give us to run it properly. Furthermore, my classmate run the same code (what I add) on his laptop and it works. Excuse me, I know my English is bad...

Obviously the object files are incompatible either because your build process is broken or if your professor just gives you the compiled object file, because it is not ABI compatible with your implementation i.e. compiler, OS, architecture.

Related

Looking for source code of __builtin_avr_delay_cycles called by _delay_ms in avr-gcc

I was investigating the delay_ms function of avr-gcc. In delay.h I found its definition:
void _delay_ms(double __ms)
{
double __tmp ;
#if __HAS_DELAY_CYCLES && defined(__OPTIMIZE__) && \
!defined(__DELAY_BACKWARD_COMPATIBLE__) && \
__STDC_HOSTED__
uint32_t __ticks_dc;
extern void __builtin_avr_delay_cycles(unsigned long);
__tmp = ((F_CPU) / 1e3) * __ms;
#if defined(__DELAY_ROUND_DOWN__)
__ticks_dc = (uint32_t)fabs(__tmp);
#elif defined(__DELAY_ROUND_CLOSEST__)
__ticks_dc = (uint32_t)(fabs(__tmp)+0.5);
#else
//round up by default
__ticks_dc = (uint32_t)(ceil(fabs(__tmp)));
#endif
__builtin_avr_delay_cycles(__ticks_dc);
#else
...
}
I am interested in how the __builtin_avr_delay_cycles function looks like internally and where it is defined? Where can I find the source?
As said in my comment to this very question on electronics.SE:
Compiler builtins are kinda funky to find, always, because they are not just C functions, but things that get inserted while parsing/compiling the code (at various levels of abstraction from the textual representation of the code itself. compiler theory stuff). What you're looking for is the function avr_expand_builtin in the GCC source tree. There's a case AVR_BUILTIN_DELAY_CYCLES in there. Look for what happens there.
Which is:
/* Implement `TARGET_EXPAND_BUILTIN'. */
/* Expand an expression EXP that calls a built-in function,
with result going to TARGET if that's convenient
(and in mode MODE if that's convenient).
SUBTARGET may be used as the target for computing one of EXP's operands.
IGNORE is nonzero if the value is to be ignored. */
static rtx
avr_expand_builtin (tree exp, rtx target,
rtx subtarget ATTRIBUTE_UNUSED,
machine_mode mode ATTRIBUTE_UNUSED,
int ignore)
{
tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
const char *bname = IDENTIFIER_POINTER (DECL_NAME (fndecl));
unsigned int id = DECL_FUNCTION_CODE (fndecl);
const struct avr_builtin_description *d = &avr_bdesc[id];
tree arg0;
rtx op0;
gcc_assert (id < AVR_BUILTIN_COUNT);
switch (id)
{
case AVR_BUILTIN_NOP:
emit_insn (gen_nopv (GEN_INT (1)));
return 0;
case AVR_BUILTIN_DELAY_CYCLES:
{
arg0 = CALL_EXPR_ARG (exp, 0);
op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
if (!CONST_INT_P (op0))
error ("%s expects a compile time integer constant", bname);
else
avr_expand_delay_cycles (op0);
return NULL_RTX;
}
…
thus, the function you're looking for is avr_expand_delay_cycles in the same file:
static void
avr_expand_delay_cycles (rtx operands0)
{
unsigned HOST_WIDE_INT cycles = UINTVAL (operands0) & GET_MODE_MASK (SImode);
unsigned HOST_WIDE_INT cycles_used;
unsigned HOST_WIDE_INT loop_count;
if (IN_RANGE (cycles, 83886082, 0xFFFFFFFF))
{
loop_count = ((cycles - 9) / 6) + 1;
cycles_used = ((loop_count - 1) * 6) + 9;
emit_insn (gen_delay_cycles_4 (gen_int_mode (loop_count, SImode),
avr_mem_clobber()));
cycles -= cycles_used;
}
if (IN_RANGE (cycles, 262145, 83886081))
{
loop_count = ((cycles - 7) / 5) + 1;
if (loop_count > 0xFFFFFF)
loop_count = 0xFFFFFF;
cycles_used = ((loop_count - 1) * 5) + 7;
emit_insn (gen_delay_cycles_3 (gen_int_mode (loop_count, SImode),
avr_mem_clobber()));
cycles -= cycles_used;
}
if (IN_RANGE (cycles, 768, 262144))
{
loop_count = ((cycles - 5) / 4) + 1;
if (loop_count > 0xFFFF)
loop_count = 0xFFFF;
cycles_used = ((loop_count - 1) * 4) + 5;
emit_insn (gen_delay_cycles_2 (gen_int_mode (loop_count, HImode),
avr_mem_clobber()));
cycles -= cycles_used;
}
if (IN_RANGE (cycles, 6, 767))
{
loop_count = cycles / 3;
if (loop_count > 255)
loop_count = 255;
cycles_used = loop_count * 3;
emit_insn (gen_delay_cycles_1 (gen_int_mode (loop_count, QImode),
avr_mem_clobber()));
cycles -= cycles_used;
}
while (cycles >= 2)
{
emit_insn (gen_nopv (GEN_INT (2)));
cycles -= 2;
}
if (cycles == 1)
{
emit_insn (gen_nopv (GEN_INT (1)));
cycles--;
}
}
Of biggest interest here is that this modifies a node in the Abstract Syntax Tree, and emits instructions there.

gsl gnu solving first order ordinary differential equation

I visited the gnu gsl website and i dont find the example there to solve a differential equation to be intuitive at all (especially because it is using 2nd order differential equation). https://www.gnu.org/software/gsl/manual/html_node/ODE-Example-programs.html#ODE-Example-programs
Can somebody guide about where to find a descriptive guide to how solve a very simple first order differetial equation.
For example, supoose my function is y'=x+2y (or any such function) then how do i write code in gsl to solve it with a given fixed step size and initial condition.
For y'=f(x,y)=x+2y the arrays have all dimension 1, which normally is something to avoid, but here it is instructional. For the explicit solvers, i.e., those not containing imp in the name, you do not need the Jacobian:
#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>
int odefunc (double x, const double y[], double f[], void *params)
{
f[0] = x+2*y[0];
return GSL_SUCCESS;
}
int * jac;
int main ()
{
int dim = 1;
gsl_odeiv2_system sys = {odefunc, NULL, dim, NULL};
gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rkf45, 1e-6, 1e-6, 0.0);
int i;
double x0 = 0.0, xf = 100.0; /* start and end of integration interval */
double x = x0;
double y[1] = { 1.0 }; /* initial value */
for (i = 1; i <= 100; i++)
{
double xi = x0 + i * (xf-x0) / 100.0;
int status = gsl_odeiv2_driver_apply (d, &x, xi, y);
if (status != GSL_SUCCESS)
{
printf ("error, return value=%d\n", status);
break;
}
printf ("%.8e %.8e\n", x, y[0]);
}
gsl_odeiv2_driver_free (d);
return 0;
}
You may want to look up the book "Introduction to Computational Modeling Using C and Open-Source Tools" by Jose M. Garrido.
Lutzl, Please review:
'#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>
int odefunc (double x, const double y[], double f[], void *params)
{
f[0] = x+2*y[0];
return GSL_SUCCESS;
}
int jac(double x , const double y[] ,double *dfdy , double dfdx[], void *params) {
gsl_matrix_view dfdy_mat= gsl_matrix_view_array(dfdy,1,1);
gsl_matrix *m= &dfdy_mat.matrix;
gsl_matrix_set(m,0,0,x);
dfdx[0]=2;
return GSL_SUCCESS;
}
int main ()
{
int dim =1;
gsl_odeiv2_system sys = {odefunc, jac, dim, NULL};
gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rk1imp,1e-7,1e-7, 0.0);
int i;
double x0 = 0.0, xf =1.0; /*al value */
while(x<xf)
{
double xi = x0 + 0.25;
int status = gsl_odeiv2_driver_apply (d, &x, xi, y);
if (status != GSL_SUCCESS)
{
printf ("error, return value=%d\n", status);
break;
}
printf ("%.8e %.8e\n", x, y[0]);
}
gsl_odeiv2_driver_free (d);
return 0;
}
'

Getting “./a.out” terminated by signal SIGSEGV (Address boundary error)

I'm writing a program that splits any two numbers. The problem is whenever I run the program I get an error that says:
“./a.out” terminated by signal SIGSEGV (Address boundary error)
And that error occurs at the lines:
a = std::stoi(temp_vec.front());
b = std::stoi(temp_vec.back());
and
c = std::stoi(temp_vec.front());
d = std::stoi(temp_vec.back());
Here's my program:
#include <iostream>
#include <string>
#include <vector>
void split_number(std::vector<std::string> vect, int x);
int main()
{
int x = 0, y = 0, a = 0, b = 0, c = 0, d = 0;
std::vector<std::string> temp_vec;
std::cout << "Enter x: ";
std::cin >> x;
std::cout << "Enter y: ";
std::cin >> y;
split_number(temp_vec, x);
a = std::stoi(temp_vec.front());
b = std::stoi(temp_vec.back());
split_number(temp_vec, y);
c = std::stoi(temp_vec.front());
d = std::stoi(temp_vec.back());
return 0;
}
void split_number(std::vector<std::string> vect, int x)
{
vect.clear();
//1. convert x to string
std::string temp_str = std::to_string(x);
//2. calculate length
std::size_t len = temp_str.length();
std::size_t delm = 0;
if(len % 2 == 0) {
delm = len / 2;
} else {
delm = (len + 1) / 2;
}
//3. populate vector
vect.push_back(temp_str.substr(0, delm));
vect.push_back(temp_str.substr(delm + 1));
}
Any help would be appreciated.
You get the segmentation fault because your vector is empty. Your vector is empty because you pass a copy of your initial vector to split_number(). The copy is passed because the signature of split_number() says it requires a copy. Change it to:
void split_number(std::vector<std::string> & vect, int x)
The ampersand makes the vect parameter a reference parameter, and modifications will show in the calling code.

C++ operator overloading causing segmentation fault

below is the code which is giving segmentation fault for I don't know what reason. In an attempt to overload ^ operator, I am getting segmentation fault.
Here is my code.
#include <iostream>
#include <algorithm>
using namespace std;
class bigint {
public:
char val[1000000];
int msdindex;
bool iszero;
bigint( int i ) {
if( i == 0 )
iszero = true;
else {
iszero = false;
msdindex = -1;
while( i > 0 ) {
msdindex++;
val[ msdindex ] = i % 10;
i /= 10;
}
}
}
bigint( const bigint& bi ) {
msdindex = bi.msdindex;
iszero = bi.iszero;
for( int i = 0; i <= msdindex; i++ )
val[i] = bi.val[i];
}
};
bigint operator^( bigint k, int n ) {
if( n == 1 )
return bigint(k);
bigint half = k^(n/2);
return half;
}
int main()
{
bigint bi = bigint( 999 );
bigint di = bi ^ 4;
return 0;
}
Segmentation fault is in the overloaded function ^ and I am clueless of the reason. gdb says this.
Traceback (most recent call last):
File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in
from libstdcxx.v6.printers import register_libstdcxx_printers
ImportError: No module named 'libstdcxx'
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400749 in operator^(bigint, int) ()
Please help.
You are running out of memory, due to which you program crashes everytime. Reducing the status allocation of char in bigint to a smaller value will work fine.
Or use dynamic memory allocation in case you want huge char array, that will solve your problem.
Hope this helps.
class bigint {
public:
char *val;//[1000000];
int msdindex;
bool iszero;
bigint( int i ) {
if( i == 0 )
iszero = true;
else {
iszero = false;
msdindex = -1;
val = new char[1000000];
while( i > 0 ) {
msdindex++;
val[ msdindex ] = i % 10;
i /= 10;
}
}
}
bigint( const bigint& bi ) {
msdindex = bi.msdindex;
iszero = bi.iszero;
val = new char[1000000];
for( int i = 0; i <= msdindex; i++ )
val[i] = bi.val[i];
}
};
Don't forget to write destructor for this to deallocate this dynamically allocated memory. Cheers.

improper mandelbrot set output plotting

i am trying to write a code to display Mandelbrot set for the numbers between
(-3,-3) to (2,2) on my terminal.
The main function generates & feeds a complex number to analyze function.
The analyze function returns character "*" for the complex number Z within the set and "." for the numbers which lie outside the set.
The code:
#define MAX_A 2 // upperbound on real
#define MAX_B 2 // upper bound on imaginary
#define MIN_A -3 // lowerbnd on real
#define MIN_B -3 // lower bound on imaginary
#define NX 300 // no. of points along x
#define NY 200 // no. of points along y
#define max_its 50
int analyze(double real,double imag);
void main()
{
double a,b;
int x,x_arr,y,y_arr;
int array[NX][NY];
int res;
for(y=NY-1,x_arr=0;y>=0;y--,x_arr++)
{
for(x=0,y_arr++;x<=NX-1;x++,y_arr++)
{
a= MIN_A+ ( x/( (double)NX-1)*(MAX_A-MIN_A) );
b= MIN_B+ ( y/( (double)NY-1 )*(MAX_B-MIN_B) );
//printf("%f+i%f ",a,b);
res=analyze(a,b);
if(res>49)
array[x][y]=42;
else
array[x][y]=46;
}
// printf("\n");
}
for(y=0;y<NY;y++)
{
for(x=0;x<NX;x++)
printf("%2c",array[x][y]);
printf("\n");
}
}
The analyze function accepts the coordinate on imaginary plane ;
and computes (Z^2)+Z 50 times ; and while computing if the complex number explodes, then function returns immidiately else the function returns after finishing 50 iterations;
int analyze(double real,double imag)
{
int iter=0;
double r=4.0;
while(iter<50)
{
if ( r < ( (real*real) + (imag*imag) ) )
{
return iter;
}
real= ( (real*real) - (imag*imag) + real);
imag= ( (2*real*imag)+ imag);
iter++;
}
return iter;
}
So, i am analyzing 60000 (NX * NY) numbers & displaying it on the terminal
considering 3:2 ratio (300,200) , i even tried 4:3 (NX:NY) , but the output remains same and the generated shape is not even close to the mandlebrot set :
hence, the output appears inverted ,
i browsed & came across lines like:
(x - 400) / ZOOM;
(y - 300) / ZOOM;
on many mandelbrot codes , but i am unable to understand how this line may rectify my output.
i guess i am having trouble in mapping output to the terminal!
(LB_Real,UB_Imag) --- (UB_Real,UB_Imag)
| |
(LB_Real,LB_Imag) --- (UB_Real,LB_Imag)
Any Hint/help will be very useful
The Mandelbrot recurrence is zn+1 = zn2 + c.
Here's your implementation:
real= ( (real*real) - (imag*imag) + real);
imag= ( (2*real*imag)+ imag);
Problem 1. You're updating real to its next value before you've used the old value to compute the new imag.
Problem 2. Assuming you fix problem 1, you're computing zn+1 = zn2 + zn.
Here's how I'd do it using double:
int analyze(double cr, double ci) {
double zr = 0, zi = 0;
int r;
for (r = 0; (r < 50) && (zr*zr + zi*zi < 4.0); ++r) {
double zr1 = zr*zr - zi*zi + cr;
double zi1 = 2 * zr * zi + ci;
zr = zr1;
zi = zi1;
}
return r;
}
But it's easier to understand if you use the standard C99 support for complex numbers:
#include <complex.h>
int analyze(double cr, double ci) {
double complex c = cr + ci * I;
double complex z = 0;
int r;
for (r = 0; (r < 50) && (cabs(z) < 2); ++r) {
z = z * z + c;
}
return r;
}

Resources