I am newbie in C ,with Mingw32 compiler.
Right now i am creating decompiler from IL to C (Native)
The code generated (Without System.Object):
DecompileTestApplication_Program.c
#include "DecompileTestApplication_Program.h"
DecompileTestApplication_Program* DecompileTestApplication_Program__ctor( ) {
if (array__DecompileTestApplication_Program == 0) {
array__DecompileTestApplication_Program=(void**)malloc(sizeof(void*)*(capacity__DecompileTestApplication_Program=4));
}
DecompileTestApplication_Program* this;
//error: 'this' undeclared (first use in this function)
if (count__DecompileTestApplication_Program==0) {
this=(DecompileTestApplication_Program*)malloc(sizeof(DecompileTestApplication_Program));
goto RealConstructor;
}
this=(DecompileTestApplication_Program*)array__DecompileTestApplication_Program[--count__DecompileTestApplication_Program];
RealConstructor:
this->ind = 0;
this->a = 1;
this->b = 3;
//this._inherit_object_( ); //this is OOP tests ,still working on it
return this;
}
void DecompileTestApplication_Program_Main( ) {
int var_0_02;
var_0_02 = 0;
var_0_02 = ( var_0_02 + 1 );
int var_1_08;
var_1_08 = 1;
int var_2_0A;
var_2_0A = 3;
var_1_08 = ( var_1_08 + var_2_0A );
var_0_02 = ( var_0_02 + ( var_1_08 + var_2_0A ) );
DecompileTestApplication_Program_blat = ( DecompileTestApplication_Program_blat + ++DecompileTestApplication_Program_bpa );
}
void DecompileTestApplication_Program__cctor( ) {
DecompileTestApplication_Program_blat = 1;
DecompileTestApplication_Program_bpa = 4;
}
DecompileTestApplication_Program.h
#ifndef DecompileTestApplication_Program
#define DecompileTestApplication_Program
/*
Type's Name: DecompileTestApplication.Program
Time to Parse: 40.0023ms
*/
#include <stdio.h>
typedef struct {
//Variables
int ind;
int a;
int b;
} DecompileTestApplication_Program;
static int DecompileTestApplication_Program_blat;
static int DecompileTestApplication_Program_bpa;
//Methods
void DecompileTestApplication_Program_Main( );
DecompileTestApplication_Program* DecompileTestApplication_Program__ctor( );
void DecompileTestApplication_Program__cctor( );
static int count__DecompileTestApplication_Program=0;
static int capacity__DecompileTestApplication_Program=0;
static DecompileTestApplication_Program** array__DecompileTestApplication_Program=0;
#endif
#main.h
void main();
#main.cpp
//bookmart for includes
#include "DecompileTestApplication_Program.h"
void main() {
//bookmark for initialize
DecompileTestApplication_Program__cctor();
DecompileTestApplication_Program_Main();
}
The error found in the first file.
I searched the resolve for this error for awhile ,
but didn't found any.
#define DecompileTestApplication_Program
That means that everywhere you see the word DecompileTestApplication_Program, it gets removed. As such, your attempted declaration of this:
DecompileTestApplication_Program* this;
expands to
* this;
which attempts to dereference the undeclared variable this. To fix this, change the macro name.
Related
I have been learning how to use Unix functions to program in C so that I can program Semaphore functionality by scratch (without pthreads), but I am currently stuck. The man pages told me to include particular header files to use functions of interest (such as malloc, tsleep, wakeup, etc.), but when I try to run my program with the headers and method calls, I receive the following errors:
/tmp//ccg29960.o: In function `allocate_semaphore':
/tmp//ccg29960.o(.text+0x28d): undefined reference to `simple_lock_init'
/tmp//ccg29960.o: In function `down_semaphore':
/tmp//ccg29960.o(.text+0x2fb): undefined reference to `tsleep'
/tmp//ccg29960.o: In function `up_semaphore':
/tmp//ccg29960.o(.text+0x3b5): undefined reference to `wakeup'
/tmp//ccg29960.o: In function `free_semaphore':
/tmp//ccg29960.o(.text+0x43b): undefined reference to `simple_lock'
/tmp//ccg29960.o(.text+0x4af): undefined reference to `simple_unlock'
collect2: ld returned 1 exit status
The relevant code is below:
//#include <stdio.h>
//#include <stdlib.h>
#include <sys/errno.h>
#include <sys/queue.h>
//#include <sys/time.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/proc.h>
#include <sys/malloc.h>
#include <sys/lock.h>
struct entry
{
pid_t id;
SIMPLEQ_ENTRY(entry) next;
} *np;
typedef struct
{
const char* name;
pid_t process;
pid_t p_process; //parent process
int count;
SIMPLEQ_HEAD(queuehead,entry) head;
struct simplelock *slock;
} named_semaphore;
named_semaphore* s_list[64];
int num_semaphores = 0;
int main()
{
//lockinit(0, 0, 0,0, 0);
printf("Hello world\n");
return 0;
}
//... irrelevant code elided
int allocate_semaphore( const char* name, int initial_count )
{
int num_elements, i;
named_semaphore *new_s;
//perform initial checks before creating a new semaphore
//make sure the given name is an acceptable length
num_elements = sizeof(name) / sizeof(*name);
if ( num_elements > 32 )
{
return ENAMETOOLONG;
}
//make sure the given name is unique to this process
for (i = 0; i < num_semaphores; i++)
{
if (s_list[i]->process == getpid() && strcmp(s_list[i]->name, name))
{
return EEXIST;
}
}
//make sure there are no more than 64 semaphores active
if (num_semaphores >= 64)
{
return ENOMEM;
}
//create a new semaphore and add it to the collection
new_s = (named_semaphore*) malloc(sizeof(named_semaphore), 0, 0);
new_s->name = name;
new_s->process = getpid();
new_s->p_process = getppid();
new_s->count = initial_count;
s_list[num_semaphores] = new_s;
++num_semaphores;
//initialize the waiting queue
SIMPLEQ_INIT( &(new_s->head) );
//initialize its lock
simple_lock_init( new_s->slock );
//need to handle negative initial_count somehow
return (0);
}
int down_semaphore( const char* name )
{
named_semaphore* s;
s = getSemaphore( name );
if (s == NULL)
{
return (ENOENT);
}
s->count = (s->count) - 1;
if (s->count < 0)
{
//put process to sleep
tsleep(getpid(), getpriority(), 0, 0);
//add process to waiting queue
np = (struct entry *) malloc(sizeof(struct entry ));
np->id = getpid();
SIMPLEQ_INSERT_TAIL( &(s->head), np, next );
}
return 0;
}
int up_semaphore ( const char* name )
{
named_semaphore* s;
s = getSemaphore( name );
if ( s == NULL )
{
return (ENOENT);
}
s->count = (s->count) + 1;
if (s->count <= 0)
{
//wakeup longest waiting process
wakeup( (SIMPLEQ_FIRST( &(s->head) ))->id );
//remove process from waiting queue
SIMPLEQ_REMOVE_HEAD( &(s->head), np, next );
free( np );
}
return 0;
}
int free_semaphore( const char* name )
{
named_semaphore* s;
s = getSemaphore( name );
if ( s == NULL )
{
return (ENOENT);
}
simple_lock( s->slock );
while ( (np = SIMPLEQ_FIRST( &(s->head) ) ) != NULL )
{
//wakeup the process and return ECONNABORTED
//wakeup( getSemaphore( np->id ) );
SIMPLEQ_REMOVE_HEAD( &(s->head), np, next );
free( np );
}
free( s );
simple_unlock( s->slock );
}
I am not done modifying/fixing the logic of my overall program (for example, the lock()ing only happens in 1/3 of the intended methods), but it would be wonderful to understand why I am getting my current error so that I know how to fix similar ones in the future.
To me it seems like the methods do not recognize their header files or that I am missing a required piece of information so that the two can communicate.
To fix the errors, I've tried rearranging and commenting out the listed header files and also renaming the method calls in uppercase letters like they were presented in the header file documentation.
Any help or insight is appreciated, and thank you in advance!
The man pages you read... those were section 9, weren't they? Section 9 is for kernel programming. You can't call those functions unless your code is in the kernel.
So, I'm trying to make a Pebble app that generates a random string when you press a button. I'm pretty sure I have the Pebble code right, but I'm not sure what to do with this error:
/sdk2/[long stuff here]/ In function `_sbrk_r':
/home/[more long stuff]: undefined reference to `_sbrk'
collect2: error: ld returned 1 exit status
Waf: Leaving directory `/tmp/tmpX94xY7/build'
Build failed
And here's my code:
#include <pebble.h>
#include <stdlib.h>
#include <stdio.h>
Window *window;
TextLayer *text_layer;
char* one[] = {"string1", "stringone", "stringuno"};
char* two[] = {"string2", "stringtwo", "stringdos"};
char* three[] = {"string3", "stringthree", "stringtres"};
char* four[] = {"string4", "stringfour", "stringcuatro"};
int length1 = sizeof(one)/sizeof(*one);
int length2 = sizeof(two)/sizeof(*two);
int length3 = sizeof(three)/sizeof(*three);
int length4 = sizeof(four)/sizeof(*four);
char* gen()
{
char out[256];
sprintf(out, "%s, and then %s %s %s.", one[rand() % length1], two[rand() % length2], three[rand() % length3], four[rand() % length4]);
char* result = malloc(strlen(out) + 1);
strcpy(result, out);
return result;
}
static void select_click_handler(ClickRecognizerRef recognizer, void *context)
{
char* stringGen = gen();
text_layer_set_text(text_layer, stringGen);
free(stringGen);
}
static void click_config_provider(void *context)
{
window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler);
window_single_click_subscribe(BUTTON_ID_UP, select_click_handler);
window_single_click_subscribe(BUTTON_ID_DOWN, select_click_handler);
}
static void window_load(Window *window)
{
Layer *window_layer = window_get_root_layer(window);
GRect bounds = layer_get_bounds(window_layer);
text_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, bounds.size.h } });
text_layer_set_text(text_layer, "Press >>>");
text_layer_set_text_alignment(text_layer, GTextAlignmentCenter);
layer_add_child(window_layer, text_layer_get_layer(text_layer));
}
static void window_unload(Window *window)
{
text_layer_destroy(text_layer);
}
void handle_init(void)
{
window = window_create();
window_set_click_config_provider(window, click_config_provider);
window_set_window_handlers(window, (WindowHandlers) {
.load = window_load,
.unload = window_unload,
});
const bool animated = true;
window_stack_push(window, animated);
}
void handle_deinit(void)
{
text_layer_destroy(text_layer);
window_destroy(window);
}
int main(void)
{
handle_init();
app_event_loop();
handle_deinit();
}
I can't figure out why I'm getting that error. It's a simple application, I just have these little tweaks.
Thank you in advance for your help!
According to this (old) FAQ, that error happens when you try to use a C standard library function that hasn't been implemented in the SDK. If you look in the API reference, snprintf is available, but not sprintf. You can replace your call to sprintf in gen with something like
snprintf(out, 256, "%s, and then %s %s %s.", one[rand() % length1], two[rand() % length2], three[rand() % length3], four[rand() % length4]);
I just tried this out and it builds fine.
(As an aside, it may be a better a idea to declare out a global static buffer and just write over it each time, instead of constantly dynamically allocating memory.)
Fruit.h
class Fruit
{
private:
std::int no[3];
public:
void initialize();
int print_type();
};
Fruit.cpp
#include "Fruit.h"
void Fruit::initialize() {
int no[] = {1, 2, 3};
}
int Fruit::print_type() {
return type[0];
}
Main.cpp
#include <iostream>
#include "Fruit.h"
using namespace std;
int main()
{
Fruit ff;
ff.initialize();
int output = ff.print_type();
cout << output;
system("pause");
return 0;
}
Assume the required directives are included inside all the files.
At this moment, I find a problem when getting back the ouput since it will not result in "0" but a garbage value. How can I fix it without using constructor?
Sincerely hope that someone would do me a favor.
This is the way that doesn't use constructors and destructors, I hope you find it useful.
#include <iostream>
class Fruit
{
private : int* no;
public : void initialize();
public : void clean();
public : int print_type();
};
void Fruit::initialize()
{
no = new int[ 3 ];
no[ 0 ] = 1;
no[ 1 ] = 2;
no[ 2 ] = 3;
}
int Fruit::print_type()
{
return no[ 0 ];
}
void Fruit::clean()
{
delete[] no;
}
int main()
{
Fruit f;
f.initialize();
int o = f.print_type();
std::cout << o;
f.clean();
return 0;
}
Please, read about constructor in C++. You don't know about the elementary things in OOP C++.
#include "Fruit.h"
void Fruit::initialize() {
int no[] = {1, 2, 3};
}
This is not correctly. You most write this.no or no.
int Fruit::print_type() {
return type[0];
}
What is variable type?
int getSpeedOfMotorInPercent(int RPM)
{
int speedOfMotor = (RPM/5000.0)*100;
return speedOfMotor;
}
static char *test_GetSpeedOfMotor(int speedInPercent)
{
mu_assert("error, RPM != 70%", speedInPercent == 70);
return 0;
}
static char *run_all_tests(int RPM)
{
mu_run_test(test_GetSpeedOfMotor(RPM));
return 0;
}
I get the error "called object is not a function" on mu_run_test(test_GetSpeedOfMotor(RPM));
I tried removing the pointer of the function but then I get even more errors.
EDIT:
#define mu_assert(message, test) do { if (!(test)) return message; } while (0)
#define mu_run_test(test) do { char *message = test(); tests_run++; if (message) return message; } while (0)
extern int tests_run;
this is the mu_run_test function. It is provided to me like that in the header file.
You're passing test_GetSpeedOfMotor(RPM) as test in the macro, which will result in this code:
char *message = test_GetSpeedOfMotor(RPM)();
Since you're probably using a test framework which you don't want to change, just remove the RPM parameter from the declaration of test_GetSpeedOfMotor function and use it like this:
int testRpmInPercent;
static char *test_GetSpeedOfMotor()
{
mu_assert("error, RPM != 70%", testRpmInPercent == 70);
return 0;
}
static char *run_all_tests(int RPM)
{
testRpmInPercent = RPM;
mu_run_test(test_GetSpeedOfMotor);
return 0;
}
Then you'll have to find an other way of sharing the RPM value with the test function. Like a global variable or with whatever method the test framework has to offer.
If you're willing to change the test framework, I would modify that define to this (remove () after test):
#define mu_run_test(test) do { char *message = test; tests_run++; if (message) return message; } while (0)
I have two files: p1.c and p2.c.
I need to use the value stored in the structure in p1.c into p2.c. Please help me figure out how to achieve this. Should I use extern?
p1.c
typedef struct What_if
{
char price[2];
} what_if ;
int main()
{
what_if what_if_var[100];
file * infile;
infile=fopen("filepath");
format_input_records();
}
int format_input_records()
{
if ( infile != NULL )
{
char mem_buf [500];
while ( fgets ( mem_buf, sizeof mem_buf, infile ) != NULL )
{
item = strtok(mem_buf,delims);
strcpy(what_if_var[line_count].price,item) ;
printf("\ntrans_Indicator ==== : : %s",what_if_var[0].price);
}
}
}
p2.c
"what_if.h" // here i include the structure
int main()
{
process_input_records(what_if_var);
}
int process_input_records(what_if *what_if_var)
{
printf("\nfund_price process_input_records ==== : : %s",what_if_var[0]->price);
return 0;
}
Try this:
whatif.h:
#ifndef H_WHATIF_INCLUDED
#define H_WHATIF_INCLUDED
struct whatif {
char price[2];
};
int wi_process(struct whatif *);
#endif
p1.c
#include "whatif.h"
int main(void) {
struct whatif whatif[100];
whatif[0].price[0] = 0;
whatif[0].price[1] = 1;
whatif[1].price[0] = 42;
whatif[1].price[1] = 74;
whatif[99].price[0] = 99;
whatif[99].price[1] = 100;
wi_process(whatif);
return 0;
}
p2.c
#include <stdio.h>
#include "whatif.h"
int wi_process(struct whatif *arr) {
printf("%d => %d\n", arr[0].price[0], arr[0].price[1]);
printf("%d => %d\n", arr[1].price[0], arr[1].price[1]);
printf("%d => %d\n", arr[99].price[0], arr[99].price[1]);
return 3;
}
Then compile and link all of them together, for example with gcc
gcc -ansi -pedantic -Wall p1.c p2.c