Trying to run simple graphics program in c using command prompt - c

This is the following program.
#include <graphics.h>
int main(void)
{
drawRect(50,100,200,150);
drawLine(50,100,150,25);
drawLine(150,25,250,100);
drawRect(130,190,40,60);
drawRect(70,195,40,30);
drawRect(190,195,40,30);
drawRect(70,125,40,30);
drawRect(190,125,40,30);
return 0;
}
and following is the error
C:\Users\Balavardhan\Desktop\New folder\c language>gcc z.c -o z.exe
z.c: In function 'main':
z.c:4:6: warning: implicit declaration of function 'drawRect'; did you mean 'drawpoly'? [-Wimplicit-function-declaration]
drawRect(50,100,200,150);
^~~~~~~~
drawpoly
z.c:5:6: warning: implicit declaration of function 'drawLine'; did you mean 'drawpoly'? [-Wimplicit-function-declaration]
drawLine(50,100,150,25);
^~~~~~~~
drawpoly
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x2e): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x52): undefined reference to `drawLine'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x76): undefined reference to `drawLine'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x9a): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0xbe): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0xe2): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x106): undefined reference to `drawRect'
C:\Users\BALAVA~1\AppData\Local\Temp\ccMnl0ZM.o:z.c:(.text+0x12a): undefined reference to `drawRect'
collect2.exe: error: ld returned 1 exit status
Can anyone tell me how can I solve this problem?

Related

CUDD package : Undefined reference during compilation

I am learning CUDD package for research purposes. I have got one sample code from which I have tried to learn the basic functionalities. But I am getting error during compilation.
I have already set the paths for the header.
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include "cudd.h"
#include "util.h"
void print_dd(DdManager *gbm, DdNode *dd, int n, int pr)
{
printf("Ddmanager nodes : %ld \n",Cudd_ReadNodeCount(gbm));
printf("Ddmanager vars : %d \n",Cudd_ReadSize(gbm));
printf("Ddmanager reorderings :%d\n",Cudd_ReadReorderings(gbm));
printf("DdManager memory % ld",Cudd_ReadMemoryInUse(gbm));
Cudd_PrintDebug(gbm,dd,n,pr);
}
void write_dd(DdManager *gbm, DdNode *dd, char * filename)
{
FILE *outfile;
outfile=fopen(filename,"w");
DdNode **ddnodearray=(DdNode **)malloc(sizeof(DdNode*));
ddnodearray[0]=dd;
Cudd_DumpDot(gbm,1,ddnodearray,NULL,NULL,outfile);
free(ddnodearray);
fclose(outfile);
}
int main(int argc, char *argv[])
{
DdManager *gbm;
char filename[30];
gbm=Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
DdNode *bdd=Cudd_bddNewVar(gbm);
Cudd_Ref(bdd);
bdd=Cudd_BddToAdd(gbm,bdd);
print_dd(gbm,bdd,2,4);
sprintf(filename,"./bdd/graph.dot");
write_dd(gbm,bdd,filename);
Cudd_Quit(gbm);
return 0;
}
I am getting some error during compilation.
gcc -I /home/subhadip/cudd-3.0.0 -I /home/subhadip/cudd-3.0.0/util -I /home/subhadip/cudd-3.0.0/cudd transfer1.c /home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a -o transfer1
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAPI.o): In function `Cudd_ExpectedUsedSlots':
/home/subhadip/cudd-3.0.0/cudd/cuddAPI.c:1835: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/cuddAPI.c:1844: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/cuddAPI.c:1850: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddCache.o): In function `cuddCacheProfile':
/home/subhadip/cudd-3.0.0/cudd/cuddCache.c:816: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddUtil.o): In function `Cudd_CountMinterm':
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddUtil.o): In function `Cudd_LdblCountMinterm':
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:729: undefined reference to `powl'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddUtil.o): In function `Cudd_CountMinterm':
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/cuddUtil.c:595: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-epd.o): In function `EpdNormalizeDecimal':
/home/subhadip/cudd-3.0.0/epd/epd.c:834: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/epd/epd.c:834: undefined reference to `pow'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-epd.o):/home/subhadip/cudd-3.0.0/epd/epd.c:452: more undefined references to `pow' follow
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAnneal.o): In function `siftBackwardProb':
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:671: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:671: undefined reference to `exp'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAnneal.o): In function `cuddAnnealing':
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:229: undefined reference to `log'
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:229: undefined reference to `log'
/home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a(cudd_libcudd_la-cuddAnneal.o): In function `siftBackwardProb':
/home/subhadip/cudd-3.0.0/cudd/cuddAnneal.c:671: undefined reference to `exp'
collect2: error: ld returned 1 exit status
I have tried to statically link the libraries but the there is a problem. How can I fix it?
You compiled cudd and generated a static library. Now you need to link with it:
gcc .. <other options> ... transfer1.c /home/subhadip/cudd-3.0.0/cudd/.libs/libcudd.a -o transfer1
Note that the order of files matter.
I can guess that for C++ support you have to link with cplusplus/.libs/libobj.a and for dddmp support you need symbols exported in dddmp/.libs/libdddmp.a.
After using the above solution, if the error still persists,
For me, I have not made the folder named 'bdd' in the proper location for the code line:
sprintf(filename, "./bdd/graph.dot");
Now, it is executing for me.

load lua lib in a c app

I want to make so that I can call lua functions from my C program but I dont know how to load the lua library.
So far I have tried to download all the lua source code from lua.org, and then put them into the same folder as my C program. And then included them in the C code.
How my code looks now:
#include "lua.h"
#include "lauxlib.h"
int main()
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_dofile(L, "test.lua");
return 0;
}
Error msg when compiling:
test.c: In function ‘main’:
test.c:10:5: warning: implicit declaration of function ‘luaL_openlibs’ [->Wimplicit-function-declaration]
luaL_openlibs(L);
^
In file included from test.c:5:0:
lauxlib.h:122:24: warning: value computed is not used [-Wunused-value]
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
^
test.c:11:5: note: in expansion of macro ‘luaL_dofile’
luaL_dofile(L, "test.lua");
^
/tmp/ccHJTAY8.o: In function main':
test.c:(.text+0x9): undefined reference toluaL_newstate'
test.c:(.text+0x1e): undefined reference to luaL_openlibs'
test.c:(.text+0x34): undefined reference toluaL_loadfilex'
test.c:(.text+0x5f): undefined reference to `lua_pcallk'
collect2: error: ld returned 1 exit status

C library linking in linux

I have been working on a C code for which I needed some functions of libxls.h header files. I have already downloaded the corresponding files and followed the installation instruction. The files also contained a test C code (test.c) which I tried compiling. The results were as follow:
Command to compile the test.c file-
gcc test.c -lm
Results:
[root#XXXXXXX95549 test]# gcc test.c -lm
test.c:28:24: error: libxls/xls.h: No such file or directory
test.c: In function ‘main’:
test.c:33: error: ‘xlsWorkBook’ undeclared (first use in this function)
test.c:33: error: (Each undeclared identifier is reported only once
test.c:33: error: for each function it appears in.)
test.c:33: error: ‘pWB’ undeclared (first use in this function)
test.c:34: error: ‘xlsWorkSheet’ undeclared (first use in this function)
test.c:34: error: ‘pWS’ undeclared (first use in this function)
test.c:39: error: ‘WORD’ undeclared (first use in this function)
test.c:39: error: expected ‘;’ before ‘t’
test.c:53: error: ‘t’ undeclared (first use in this function)
test.c:58: error: ‘tt’ undeclared (first use in this function)
test.c:60: error: dereferencing pointer to incomplete type
test.c:63: error: dereferencing pointer to incomplete type
test.c:64: error: dereferencing pointer to incomplete type
test.c:66: error: dereferencing pointer to incomplete type
test.c:67: error: dereferencing pointer to incomplete type
test.c:68: error: dereferencing pointer to incomplete type
test.c:70: error: dereferencing pointer to incomplete type
test.c:70: error: dereferencing pointer to incomplete type
test.c:71: error: dereferencing pointer to incomplete type
To solve this problem, I copied the library files at the '/user/include' location
After which the results for same compilation commands were:
[root#XXXXXX95549 test]# gcc test.c -lm
/tmp/cc8DJETV.o: In function `main':
test.c:(.text+0x14): undefined reference to `xls_open'
test.c:(.text+0xd4): undefined reference to `xls_getWorkSheet'
test.c:(.text+0xe4): undefined reference to `xls_parseWorkSheet'
test.c:(.text+0xf0): undefined reference to `xls_getCSS'
test.c:(.text+0x49c): undefined reference to `xls_showBookInfo'
collect2: ld returned 1 exit status
Please, explain how can I rectify this problem.
The code test.c is given below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <libxls/xls.h>
int main()
{
xlsWorkBook* pWB;
xlsWorkSheet* pWS;
FILE *f;
int i;
struct st_row_data* row;
WORD t,tt;
pWB=xls_open("files/test2.xls", "ASCII"); // "KOI8-R"
if (pWB!=NULL)
{
f=fopen ("test.htm", "w");
for (i=0;i<pWB->sheets.count;i++)
printf("Sheet N%i (%s) pos %i\n",i,pWB->sheets.sheet[i].name,pWB->sheets.sheet[i].filepos);
pWS=xls_getWorkSheet(pWB,0);
xls_parseWorkSheet(pWS);
fprintf(f,"<style type=\"text/css\">\n%s</style>\n",xls_getCSS(pWB));
fprintf(f,"<table border=0 cellspacing=0 cellpadding=2>");
for (t=0;t<=pWS->rows.lastrow;t++)
{
row=&pWS->rows.row[t];
// xls_showROW(row->row);
fprintf(f,"<tr>");
for (tt=0;tt<=pWS->rows.lastcol;tt++)
{
if (!row->cells.cell[tt].ishiden)
{
fprintf(f,"<td");
if (row->cells.cell[tt].colspan)
fprintf(f," colspan=%i",row->cells.cell[tt].colspan);
// if (t==0) fprintf(f," width=%i",row->cells.cell[tt].width/35);
if (row->cells.cell[tt].rowspan)
fprintf(f," rowspan=%i",row->cells.cell[tt].rowspan);
fprintf(f," class=xf%i",row->cells.cell[tt].xf);
fprintf(f,">");
if (row->cells.cell[tt].str!=NULL && row->cells.cell[tt].str[0]!='\0')
fprintf(f,"%s",row->cells.cell[tt].str);
else
fprintf(f,"%s"," ");
fprintf(f,"</td>");
}
}
fprintf(f,"</tr>\n");
}
fprintf(f,"</table>");
printf("Count of rows: %i\n",pWS->rows.lastrow);
printf("Max col: %i\n",pWS->rows.lastcol);
printf("Count of sheets: %i\n",pWB->sheets.count);
fclose(f);
xls_showBookInfo(pWB);
}
return 0;
}
Thanks in advance,
Got the answer to this question on the following location:
http://www.cprogramdevelop.com/2018568/
Those who might be facing the problem with libxls library can refer the above mentioned website.
cheers

Undefined reference error after using Makefile

I wrote a makefile to link all the .c files that i wanted to.But then again i get errors of undefined reference.The code for the makefile is:
FILES.o=set.o hash.o nfa.o printnfa.o input.o terp.o dfa.o minimize.o defnext.o print_ar.o pairs.o squash.o print.o assort.o prnt.o printv.o bintoasc.o ferr.o onferr.o fputstr.o pchar.o driver.o searchenv.o hashadd.o
PROGRAM= lexer
all: ${PROGRAM}
${PROGRAM}: ${FILES.o}
${CC} -o $# ${CFLAGS} $^ ${LDFLAGS} ${LDLIBS}
But still it leads to undefined errors like :
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':(.text+0x20): undefined reference to `main'
nfa.o: In function `parse_err':
nfa.c:(.text+0x21): undefined reference to `Actual_lineno'
nfa.o: In function `save':
nfa.c:(.text+0x2d1): undefined reference to `Lineno'
nfa.o: In function `advance':
nfa.c:(.text+0x8b4): undefined reference to `esc'
nfa.o: In function `rule':
nfa.c:(.text+0xae1): undefined reference to `Unix'
nfa.o: In function `term':
nfa.c:(.text+0xfbd): undefined reference to `Unix'
nfa.c:(.text+0x10a2): undefined reference to `Unix'
nfa.o: In function `thompson':
nfa.c:(.text+0x1355): undefined reference to `CLEAR_STACK'
nfa.c:(.text+0x13ab): undefined reference to `Verbose'
nfa.c:(.text+0x13d3): undefined reference to `printf_nfa'
nfa.c:(.text+0x13d9): undefined reference to `Verbose'
input.o: In function `get_expr':
input.c:(.text+0x13): undefined reference to `Input_buf'
input.c:(.text+0x20): undefined reference to `Verbose'
input.c:(.text+0x2b): undefined reference to `Actual_lineno'
input.c:(.text+0x52): undefined reference to `Actual_lineno'
input.c:(.text+0x7a): undefined reference to `Actual_lineno'
input.c:(.text+0x83): undefined reference to `Actual_lineno'
input.c:(.text+0x8a): undefined reference to `Input_buf'
input.c:(.text+0x93): undefined reference to `Input_buf'
input.c:(.text+0xe2): undefined reference to `Ifile'
input.c:(.text+0x10c): undefined reference to `Verbose'
input.c:(.text+0x11c): undefined reference to `Input_buf'
input.c:(.text+0x136): undefined reference to `Input_buf'
dfa.o: In function `dfa':
dfa.c:(.text+0x69): undefined reference to `Verbose'
dfa.c:(.text+0x1c6): undefined reference to `Verbose'
dfa.c:(.text+0x215): undefined reference to `Verbose'
dfa.o: In function `get_unmarked':
dfa.c:(.text+0x3a8): undefined reference to `Verbose'
minimize.o: In function `init_groups':
minimize.c:(.text+0x28c): undefined reference to `Verbose'
minimize.o:minimize.c:(.text+0x550): more undefined references to `Verbose' follow
print.o: In function `pdriver':
print.c:(.text+0x483): undefined reference to `No_lines'
print.c:(.text+0x4eb): undefined reference to `No_lines'
print.c:(.text+0x4f6): undefined reference to `Input_file_name'
print.c:(.text+0x585): undefined reference to `No_lines'
collect2: error: ld returned 1 exit status
make: *** [lexer] Error 1
All the undefined references are however present in 2 header files :1.stack.h 2.global.h..Still the errors.please help!
The global.h file i am using is:
#ifndef __GLOBAL_H
#define __GLOBAL_H
#include <stdio.h>
#ifdef ALLOC
# define CLASS
# define I(x) x
#else
# define CLASS extern
# define I(x)
#endif
#define MAXINP 2048
CLASS int Verbose I(=0);
CLASS int No_lines I(=0);
CLASS int Unix I(=0);
CLASS int Public I(=0);
CLASS char *Template I(="lex.par");
CLASS int Actual_lineno I(=1);
CLASS int Lineno I(=1);
CLASS char Input_buf[MAXINP]; //line buffer for input
CLASS char *Input_file_name; //Input file name
CLASS FILE *Ifile; //Input Stream
CLASS FILE *ofile; //Output Stream
#endif
Your problem isn't related to the Makefile per se. The header-only include file global.h contains declarations of variables. These declarations have to be turned into actual definitions in one compilation unit, so that they have space for them allocated in one of your object files.
The way your header file is designed, the definition of the variable ALLOC determines whether the variables are just declared or defined.
The regular include without ALLOC yields for example:
extern int Verbose;
The extern keyword indicates that the variable Verbose is not defined. No memory is allocated for it (and it therefore can't have an initialiser) and that it is probably defined in another object file.
When ALLOC is defined, the declaration becomes a definition with initialisation:
int Verbose = 0;
Pick one of the files that include global.h and define ALLOC before including it, for example:
input.c
#define ALLOC
#include "global.h"
You should then have the symbol Verbose defined as an int with initial value 0 in global.o:
> nm input.o | grep Verbose
0000000000000000 B Verbose
> nm set.o | grep Verbose
U Verbose
(nm is a Linux utility that lists the symbols in an object file. Windows has dumpbin.)
The U in the object file without ALLOC means that the symbol is undefined, i.e. referenced but not defined in this object file. The B in input.o denotes the section where the symol is defined. When you link, you can have many Us, but for each symbol that is undefined in an object file, you also need one object file where the symbol is defined.
you should add header files in all files which are contained in makefile...
otherwise those files will not able to recognize those variables

C sockets error

For some reason this line of code is giving me quite a problem.
struct socketaddr_in clientaddr;
The error message is:
tiny.c:23:24: error: storage size of ‘clientaddr’ isn’t known
If I remove that line of code I get the following error message:
s2s2#s2s2-ThinkPad-T61:~/Documents/Cprogramming/web_server$ make
gcc -std=gnu99 -O2 -lpthread -lrt -o server tiny.c csapp.c
/tmp/ccVxw07i.o: In function `Pthread_create':
csapp.c:(.text+0x7e5): undefined reference to `pthread_create'
/tmp/ccVxw07i.o: In function `Pthread_cancel':
csapp.c:(.text+0x805): undefined reference to `pthread_cancel'
/tmp/ccVxw07i.o: In function `Pthread_join':
csapp.c:(.text+0x825): undefined reference to `pthread_join'
/tmp/ccVxw07i.o: In function `Pthread_detach':
csapp.c:(.text+0x845): undefined reference to `pthread_detach'
/tmp/ccVxw07i.o: In function `Sem_init':
csapp.c:(.text+0x895): undefined reference to `sem_init'
/tmp/ccVxw07i.o: In function `P':
csapp.c:(.text+0x8b5): undefined reference to `sem_wait'
/tmp/ccVxw07i.o: In function `V':
csapp.c:(.text+0x8d5): undefined reference to `sem_post'
/tmp/ccVxw07i.o: In function `Pthread_once':
csapp.c:(.text+0x881): undefined reference to `pthread_once'
collect2: ld returned 1 exit status
make: *** [webServer-gcc] Error 1
Here are links to the csapp.c and csapp.h files.
Thanks for all the help.
tiny.c:23:24: error: storage size of ‘clientaddr’ isn’t known
There's a reason you need to declare structs as struct structname instancename in C - that's so that the C compiler knows how much memory to allocate - and possibly how to align that data, etc.
This is the C compiler's way of telling you no such struct socketaddr_in exists - it'd be sockaddr_in.
A common way to work around the naming of structs in this way is to define them like this:
typedef struct _struct_name
{
/* ... */
} structname;
then structname can be used as a type without the struct qualifier. You don't have to do this on the definition of the struct, either, you could do it later.
So, the short answer is socketaddr_in doesn't exist as a struct in POSIX - it's sockaddr_in.

Resources