Here is the basic c example from the documentation:
#include <mgl2/mgl_cf.h>
int sample(HMGL gr, void *)
{
mgl_rotate(gr,60,40,0);
mgl_box(gr);
}
int main(int argc,char **argv)
{
HMGL gr;
gr = mgl_create_graph_qt(sample,"MathGL examples",0,0);
return mgl_qt_run();
/* generally I should call mgl_delete_graph() here,
* but I omit it in main() function. */
}
Here is the start of compilation output:
$ gcc test.c -lmgl-qt5 -lmgl
In file included from /usr/include/mgl2/mgl_cf.h:29,
from test.c:1:
/usr/include/mgl2/data_cf.h:527:17: error: expected ‘,’ or ‘;’ before ‘mgl_find_roots’
527 | bool MGL_EXPORT mgl_find_roots(size_t n, void (*func)(const mreal *x, mreal *f, void *par), mreal *x0, void *par);
| ^~~~~~~~~~~~~~
test.c: In function ‘sample’:
test.c:2:21: error: parameter name omitted
2 | int sample(HMGL gr, void *)
| ^~~~~~
It seems clear to me that the example is not even valid c, missing a parameter (that is not actually used) to the sample() function. I have tried removing it but still get the first (internal mathgl) error.
Any ideas how to proceed?
It seems MathGL doesn't have their internal #include statements in order, and require you to be careful about what you #include and in what order. In particular, ensure you #include <mgl2/mgl.h> before any other MathGL header, and before that one ensure you #include <stdbool.h>. Futhermore, when you use for example Qt-related functions, ensure you #include <mgl2/qt.h>. This should work:
#include <stdbool.h>
#include <mgl2/mgl.h>
#include <mgl2/qt.h>
int sample(HMGL gr, void *ignored)
{
mgl_rotate(gr,60,40,0);
mgl_box(gr);
}
int main(int argc, char **argv)
{
HMGL gr = mgl_create_graph_qt(sample, "MathGL examples", 0, 0);
return mgl_qt_run();
}
Related
Say I have a library I am making and I want to call a function rename or puts, how can I keep the original rename and puts from stdlib or stdio or whatever, and yet have my own function be puts?
#include <stdio.h>
alias puts original_puts;
void
puts(char *c) {
original_puts(c);
}
How can I accomplish something to this effect?
You can't alias library functions, but you can alias your own using preprocessor directives.
For example:
mylib.h:
#include <stdio.h>
void my_puts(char *c);
#define puts(arg) my_puts(arg)
mylib.c:
void my_puts(char *c)
{
(puts)(c);
}
Now, anytime someone calls puts, it substitutes a call to my_puts instead. Also, when you want to call the "real" function in your wrapper, you can put the function name in quotes. Because the macro that does the substitution is a function-like macro, the parenthesis prevent the substitution from happening.
If compiling with gcc or clang, you can wrap the symbol with -Wl,--wrap:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int __real_puts(const char *c);
int __wrap_puts(const char *c) {
__real_puts("Hello");
__real_puts(c);
return 0;
}
int main(int argc, char const *argv[]) {
puts("world");
}
$ gcc src.c -Wl,--wrap=puts && ./a.out
Hello
world
Hello folks out there,
this is my code:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "db_typedefs.h"
#include "operations.h"
int main(){
printf("Text\n");
int f = 3;
void add_mini(3);
}
operations.h
#ifndef ADD_OPERATIONS_H_INCLUDED
#define ADD_OPERATIONS_H_INCLUDED
void add_mini(int flag);
#endif // ADD_OPERATIONS_H_INCLUDED
operations.c
void add_mini(int flag)
{
int rc;
rc = flag;
printf("%i\n", rc);
}
Operations.c has also libraries included similar to main.c.
Compiler Error
error: expected declaration specifiers or '...' before numeric constant
regarding to void add_mini(3)
It seems like I'm unable to pass a simple integer value. While debugging it's even skipping the add_mini line.
Do you have any idea what's going on?
The whole code is embedded in a larger query to determine typed orders but this works fine. I just can't pass this simple integer value.
Thanks in advance.
When you use
void add_mini(3);
the compiler thinks it is a function declaration, not a function call. The argument 3 is not valid for a function declaration. Hence, the compiler complains.
Remove the void part to call the function.
int main(){
printf("Text\n");
int f = 3;
add_mini(3);
}
or, since you have initialized f to 3,
int main(){
printf("Text\n");
int f = 3;
add_mini(f);
}
Call the function like so: add_mini(3); rather than void add_mini(3);
Remove the word void for calling add_mini from main.c :
add_mini(3);
Or
(void)add_mini(3);
When I try to compile my code, I'm getting the error:
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
I read that "Undefined symbol errors can occur when a function is declared (as is the case for the lock functions in lock.h), but it is not properly implemented." But I feel that I am implementing all the functions in lock.h properly since all the functions are of type voidand I don't return anything. So I don't know what I could be doing wrong to get this error.
Any help would greatly be appreciated!
Note: Please let me know if I need to provide more code. I don't think I need to write what I put in the function implementations of lock.h since I feel all you need to know is that the function implementations do not return anything, but please let me know if I need to include that.
Code
lock.c
#include "lock.h"
extern process_t * current_process;
extern process_t * process_queue;
void l_init(lock_t* l){
//Does stuff but never type return or return anything
}
void l_lock(lock_t* l){
//Does stuff but never type return or return anything
}
void l_unlock(lock_t* l){
//Does stuff but never type return or return anything
}
lock.h
#ifndef __LOCK_H_INCLUDED__
#define __LOCK_H_INCLUDED__
#include "3140_concur.h"
#include "shared_structs.h"
void l_init(lock_t* l);
void l_lock(lock_t* l);
void l_unlock(lock_t* l);
#endif /* __LOCK_H_INCLUDED */
3140_concur.c
#include "3140_concur.h"
#include <stdlib.h>
3140_concur.h
#ifndef __3140_CONCUR_H__
#define __3140_CONCUR_H__
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "process.h"
void process_blocked (void);
void process_terminated (void);
unsigned int * process_stack_init (void (*f)(void), int n);
void process_stack_free (unsigned int *sp, int n);
void process_begin (void);
#endif
process.h
#include <stdlib.h>
#include <fsl_device_registers.h>
#include "3140_concur.h"
struct process_state;
typedef struct process_state process_t;
unsigned int * process_select (unsigned int * cursp);
extern process_t * current_process;
extern process_t * process_queue;
void process_start (void);
int process_create (void (*f)(void), int n);
process.c
(I do process_t* process_queue = NULL; & process_t* current_process = NULL; b/c I want them to be NULL before any functions are called)
#include "3140_concur.h"
#include "shared_structs.h"
#include <stdlib.h>
#include <fsl_device_registers.h>
process_t* process_queue = NULL;
process_t* current_process = NULL;
lab4_t0.c
#include "process.h"
#include "utils.h"
#include "lock.h"
lock_t l;
\\uses functions in lock.h
Update
Build output
*** Using Compiler 'V5.06 update 4 (build 422)', folder: 'C:\Keil_v5\ARM\ARMCC\Bin'
Build target 'Target 1'
compiling 3140_concur.c...
compiling lab4_t0.c...
lab4_t0.c(42): warning: #111-D: statement is unreachable
return 0;
lab4_t0.c: 1 warning, 0 errors
compiling process.c...
process.c(100): warning: #1-D: last line of file ends without a newline
}
process.c: 1 warning, 0 errors
linking...
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_init (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_lock (referred from lab4_t0.o).
.\Objects\Lab4.axf: Error: L6218E: Undefined symbol l_unlock (referred from lab4_t0.o).
Not enough information to list image symbols.
Finished: 1 information, 0 warning and 3 error messages.
".\Objects\Lab4.axf" - 3 Error(s), 2 Warning(s).
Target not created.
Build Time Elapsed: 00:00:39
Thanks to #AjayBrahmakshatriya, it turns out I didn't add lock.c to my project. That fixed everything. Whew.
I try to make program in C and I cant use functions from .h without including .c file too. If I include .c after including .h it works. I get "undefined reference to ..." error on every function defined in .h.
main.c:
#include "mp.h"
//#include "mp.c"
int main()
{
int n;
printf("Unesite broj clanova niza: ");
scanf("%d",&n);
int *a=(int *)malloc(n*sizeof(int));
if (a==NULL) exit(0);
unos(a,n);
sortiranje(a,n,rastuci);
stampanje(a,n);
sortiranje(a,n,opadajuci);
stampanje(a,n);
return 0;
}
mp.h:
#ifndef MP_H_INCLUDED
#define MP_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
enum tip_sort {opadajuci,rastuci};
void unos(int *, int);
void sortiranje(int *, int, enum tip_sort);
void stampanje(int *, int);
#endif // MP_H_INCLUDED
mp.c:
#include "mp.h"
void unos(int *a, int n){
...
}
void sortiranje(int *a, int n, enum tip_sort t){
...
}
void stampanje(int *a, int n){
...
}
What you're seeing is a linker error. I guess, you're trying to compile main.c all alone.
You compilation statement should look like
gcc main.c mp.c -o output
and yes, do not #include .c (source) files. Source files are meant to be compiled and linked together to form the binary.
Note: Also, please do not cast the return value of malloc().
I wrote a code and it compiles perfectly in CodeBlocks 13.12 with no errors !
i copied the same code to VS2010 it shows 1 error:
IntelliSense: identifier "malloc" is undefined
CodeBlocks Code:
#include <stdio.h>
#define maxLength 4
typedef short int *set;
void func(set *a)
{
*a=malloc(maxLength*sizeof(set));
(*a)[0]=10;
(*a)[1]=13;
(*a)[2]=15;
}
void main()
{
set a;
func(&a);
printf("%d %d %d",a[0],a[1],a[2]);
}
VS2010 Code:
#include "stdafx.h"
#include <stdio.h>
#define maxLength 4
typedef short int *set;
void func(set *a){
*a=malloc(maxLength*sizeof(set));
(*a)[0]=10;
(*a)[1]=13;
(*a)[2]=15;
}
int _tmain(int argc, _TCHAR* argv[])
{
set a;
func(&a);
printf("%d %d %d",a[0],a[1],a[2]);
return 0;
}
I don't know what is the problem ..
and if i add in the pre-compile code : #include <iostream>
the error goes , but another error appears:
IntelliSense: a value of type "void *" cannot be assigned to an entity of type "set"
NEW CODE
void func(set *a){
func(a);
}
void func1(set *a){
*a=reinterpret_cast<set>(malloc(maxLength*sizeof(set)));
(*a)[0]=10;
(*a)[1]=13;
(*a)[2]=15;
}
How do i create an array in a function that called by a function ?
There are two problems you seem to have. The first and most serious is that you're missing a header file. See e.g. this malloc reference. Not including this header file will cause your memory allocations to now work as you expect.
The other error is that you actually seem to be using C++ and not C. In C you should not cast the return of malloc, but in C++ you must do it.
You should add a header file defining the malloc function: stdlib.h (C) or cstdlib (C++).