I'm using x64 Ubuntu Linux via VMware Player as a virtual machine. As a part of my project, I need to install some library (fec-3.0.1). I'm new to Linux and not good at coding.
Here is the error I encounter in terminal after successful configuration:
farhat#ubuntu:~/project/fatcaps_v0.5/fec-3.0.1$ make
gcc -g -O2 -I. -Wall -c -o dotprod.o dotprod.c
dotprod.c: In function ‘freedp’:
dotprod.c:56:3: error: label at end of compound statement
default:
^
make: *** [dotprod.o] Error 1
Here is function 'freedp' content (the start line is 55):
/* Free a dot product descriptor created earlier */
void freedp(void *p){
switch(Cpu_mode){
case PORT:
default:
#ifdef __i386__
case MMX:
case SSE:
return freedp_mmx(p);
case SSE2:
return freedp_sse2(p);
#endif
#ifdef __VEC__
case ALTIVEC:
return freedp_av(p);
#endif
}
}
What should I do?
The reason why you meet the error label at end of compound statement is because the default case can not be empty, it means you must provide a break or ; empty statement.
Edited: I find some material on that topic, and I got that: https://mail.gnome.org/archives/evolution-patches/2004-April/msg00235.html, It is the problem about the compiler gcc3.4, which reports an error on default without statements, and it is warning on gcc3.3, but now, I test on gcc4.8.2, it is all right....
Related
I am making a small console game and want to allow instant input and output for the player so that they don't have to hit enter after each move. I understand there are already a few solutions that can be found online, most prominently ncurses (I'm on linux), but I have not been able to get anything to work. When I input the code below, I get the message:
main.cpp:(.text+0x39c): undefined reference to `initscr'
main.cpp:(.text+0x3a3): undefined reference to `stdscr'
main.cpp:(.text+0x3ac): undefined reference to `wgetch'
main.cpp:(.text+0x3b4): undefined reference to `endwin'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I understand skipping the input buffer is not standard in C, but any ways around this would be greatly appreciated. I can make the code work by just using key = getchar(); but as aforementioned, I want to skip the necessary enter key.
void move(int key){
initscr();
key = getch();
switch(key){
case 'w': if (playery > 1){y--;}
break;
case 's': if (playery < height - 1){y++;}
break;
case 'a': if(playerx > 1){x--;}
break;
case 'd': if(playerx < width - 1){x++;}
break;
default: grid[x][y] = 'x';
break;
}
endwin();
}
You need to link with ncurses to make it work. When you include curses.h or ncurses.h, it includes just the definitions. The actual code is separated, so you have to tell your compiler to add the code to your program.
Compiling with clang
Use this when compiling C++:
clang++ main.cpp -lncurses
Use this when compiling C:
clang main.c -lncurses
Never use clang with C++ or clang++ with C. It creates a huge mess.
Compiling with GNU/gcc
Use this when compiling C++:
g++ main.cpp -lncurses
Use this when compiling C:
gcc main.c -lncurses
Again, do not interchange those.
You say you use C, but you have main.cpp, which implies you are using C++. Rename your file to main.c or edit your question.
As noted in comments, ncurses already has a function named move. You should rename your function.
I am trying to compile an application that uses freetype2, I downloaded and compiled the latest version of freetype2 from an official site.
I compile the program using this command:
gcc ttf2sfn.c -I/home/emilia/os/freetype-2.10.1/include -L/home/emilia/os/freetype-2.10.1/objs -lfreetype
However, the linker cannot find the `FT_Error_String' character.
It prints this error message:
/tmp/cc6FGxyc.o: In function `main':
ttf2sfn.c:(.text+0x25aa): undefined reference to `FT_Error_String'
ttf2sfn.c:(.text+0x352c): undefined reference to `FT_Error_String'
collect2: error: ld returned 1 exit status
Why is this happening?
Thanks.
It doesn't seem to actually exist. I use my own function, called ftstrerror, to do something equivalent:
static const char *ftstrerror(FT_Error error)
{
#undef FTERRORS_H_
#define FT_ERRORDEF(error_code, value, string) case error_code: return string;
#define FT_ERROR_START_LIST switch(error) {
#define FT_ERROR_END_LIST default: return "Unknown error"; }
#include FT_ERRORS_H
}
[Tested with FreeType 2.10.3.] You must have done something wrong. FT_Error_String is always defined. However, only if the library has been configured (at build time) with macro FT_CONFIG_OPTION_ERROR_STRINGS set, it returns non-NULL values. Since the default is off, you have to explicitly enable this feature in file include/freetype/config/ftoption.h.
Thanks , this has been solved , please see updated code and output in the answer . Thanks Jonathan and everyone else.
I have written below code to read the file present in same directory.
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
int main(){
FILE *fptr;
/*Tried putting different combinations like filename with
quotes|filename without quotes|complete path with quotes|complete path
without quotes*/
if((fptr=fopen("TestFile.txt","r"))==NULL){
printf("\nfopen() returning NULL: %d , %s \n",errno,strerror(errno));
}else{
printf("\nfopen() returning something else: %d , %s
\n",errno,strerror(errno));
}
int c;
while((c=fgetc(fptr))!=EOF){
printf("%c",c);
}}
And i am was getting below output :
./a.out
Segmentation fault (core dumped)
And a GDB core analysis had the following :
(gdb) run
Starting program: /home/astitva/Documents/Coding/a.out
Dwarf Error: wrong version in compilation unit header (is 0, should be 2,
3, or 4) [in module /usr/lib/debug/.build-
id/12/5dab90a4cfa8edc5d532f583e08e810c232cd5.debug]
warning: Could not load shared library symbols for linux-vdso.so.1.
Do you need "set solib-search-path" or "set sysroot"?
Dwarf Error: wrong version in compilation unit header (is 0, should be 2,
3, or 4) [in module /usr/lib/debug/.build-
id/c0/5201cc642f6b800835e811d7cb28f103aeb191.debug]
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7abc496 in strlen () from /lib/x86_64-linux-gnu/libc.so.6
and my text file TestFile.txt was :
DATA ENETERD AT RUN INSTANCE 1 ------> BLABLABLA
DATA ENETERD AT RUN INSTANCE 2 ------> YADAYADAYADA
DATA ENETERD AT RUN INSTANCE 3 ------> FOOBARFOOBAR
To avoid the warning, you need to #include <string.h> in your code. Add an exit(1) in the error-handling if block :
if((fptr=fopen("TestFile.txt","r"))==NULL){
printf("\nfopen() returning NULL: %d %s\n",errno, strerror(errno));
exit(1);
}
The program needs to exit "gracefully" if the file doesn't exist. So, if there is no valid file present, the program will simply exit and not print anything on the stdout.
EDIT : Just adding on Jonathan's helpful comment on ignoring compiler's warnings :
"If you ignored a compiler warning — don't. If the compiler didn't warn you about the undeclared function strerror(), you need to find the options that make it report such problems (if you use gcc, you would use gcc -Wall -Wextra -Werror — and I'd add -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wold-style-declaration too, though clang doesn't like -Wold-style-declaration)."
I've a C program as follows:
#include<stdio.h>
void main()
{
printf("Hello");
}
Filename is linkedlist.c
and makefile in the same directory as follows
build: linkedlist.c
gcc -Wall -g -pedantic linkedlist.c -o linkedlist
run:
./linkedlist
I first do M-x compile and then make build followed by M-x compile ->make run which gives output as follows:
-*- mode: compilation; default-directory: "/home/amey/test/" -*-
Compilation started at Sat Sep 22 16:59:13
make run
./linkedlist
Hello make: *** [run] Error 5
Compilation exited abnormally with code 2 at Sat Sep 22 16:59:13
The file generated when ran as ./ gives no error. Can somebody explain what is happening?
Since your main does not return a value or call exit, you are getting a random return code ... in this case, 5 (which happens, not entirely coincidentally, to be the length of "Hello" and the value returned by printf). Since it isn't 0, make interprets it as an error.
To be a conforming C program in a hosted environment, your main must be declared int, and the end of the function much not be reached (without a return statement). The interpretation of the return value is up to the implementation, but usually 0 is considered success and non-0 is considered an error.
I have this code:
#include<stdio.h>
int main()
{
int a=10;
switch(a)
{
case '1':
printf("ONE\n");
break;
case '2':
printf("TWO\n");
break;
defalut:
printf("NONE\n");
}
return 0;
}
The program doesn't print anything, not even NONE. I figured out that default had a typo defalut!
I want to know why this syntax error is not detected by the compiler.
defalut is just a label in your program that you can jump to with goto. Having an editor that highlights keywords could have made this error easier to spot.
I should also note that your program may have some logic errors. The character '1' is not the same as 1, and the same with '2' and 2.
That's not a syntax error. defalut is a valid label, and it could be the target of a goto.
tip: if you are using gcc, add the option -pedantic. it will warn you for unused labels:
$ gcc -ansi -Wall -pedantic test.c -o test
test.c: In function ‘main’:
test.c:14:10: warning: label ‘defalut’ defined but not used