Print a wide unicode character with ncurses - c

I'm trying to position a star unicode character on screen using the ncurses.h library in C on Ubuntu. The code I'm trying to run is the following:
#include <stdio.h>
#include <wchar.h>
#include <curses.h>
#include <ncurses.h>
#include <stdlib.h>
#include <wctype.h>
#include <locale.h>
int main() {
setlocale(LC_CTYPE, "");
initscr();
cbreak();
WINDOW *win = newwin(0, 0, 0, 0);
refresh();
wrefresh(win);
const wchar_t* star = L"0x2605";
mvaddwstr(3, 3, star);
getch();
endwin();
}
But I keep getting the error
implicit declaration of function ‘mvaddwstr’ [-Wimplicit-function-declaration]
Despite this function being well documented here together with similar functions which I can't get to work either. Is there some library I'm not including to make this work? or is there an alternative way to go about displaying this character? I appreciate any help.

You must be compiling against the "narrow" curses (ncurses vs ncursesw)
I was able to compile your example on ubuntu 16.04 with the following:
apt install libncursesw5-dev
# --cflags expanded to: -D_GNU_SOURCE -I/usr/include/ncursesw
gcc main.c $(ncursesw5-config --cflags) -c
# --libs expanded to: -lncursesw -ltinfo
gcc main.o $(ncursesw5-config --libs) -o main
And then
./main
I had to make the following diff to your example code as well:
- const wchar_t* star = L"0x2605";
+ const wchar_t* star = L"\x2605";

Related

Trouble compiling c gsl library from max terminal

I have a c script
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
int main(void)
{
const gsl_rng_type * T;
gsl_rng * r;
int i;
double a,b;
double num;
a=10;
b=7.2;
gsl_rng_env_setup();
T=gsl_rng_default;
r=gsl_rng_alloc (T);
for(i=0; i<10; i++)
{
num = gsl_ran_gamma(r,a,b);
printf("%.8f \n",num);
}
gsl_rng_free(r);
return 0;
}
Which I have successfully compiled on a linux machine. I want to use the gsl library for other applications on my mac. So I first installed gsl using homebrew which seemed to be successful. To make sure everything was working right I tried to compile and run this script as follows
[ACC-259-imac:GDSC Gene Expression Modeling jmannhei$ gcc -Wall gamma.c -o gamma.out -lm -lgsl -lgslcblas
which resulted in the following output
gamma.c:5:10: fatal error: 'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>
^
1 error generated.
Which is exactly how I compiled it on Linux so I am not sure what is awry as I have compile c scripts in the past using this format from terminal on a mac before. My guess is it is not linking properly but I am not sure what I need to do to fix it. Thanks
Find your header file directory with brew list gsl or locate gsl/gsl_rng.h then tell your compiler where the headers are. I would need the following, for example,
gcc -Wall -I/usr/local/Cellar/gsl/1.16/include/ gamma.c -o gamma.out -lm -lgsl -lgslcblas

gcc compile multiple files

I have these five source
main.c src_print1.c src_print2.c header_print1.h header_print2.h
the contents are simple and are as following for respective files:
main.c
#include "header_print1.h"
#include "header_print2.h"
int main(int argc, char** argv) {
print1();
print2();
return 0;
}
header_print1.h
#ifndef PRINT_1
#define PRINT_1
#include <stdio.h>
#include <stdlib.h>
void print1();
#endif
header_print2.h
#ifndef PRINT_2
#define PRINT_2
#include <stdio.h>
#include <stdlib.h>
void print2();
#endif
src_print1.c
#include "header_print1.h"
void print1() {
printf("Hello 1\n");
}
src_print2.c
#include "header_print2.h"
void print2() {
printf("Hello 2\n");
}
Using gcc I have tried to compile using the following command line:
gcc -I ./ -o test -c main.c src_print1.c src_print2.c
Everything is in the same folder.
The error I get is:
gcc: cannot specify -o with -c or -S with multiple files
I looked up at gcc manual, but actually I don't understand what to do in this case, since usually I use IDE and not the command line.
IMHO, if you rewrite your compilation statement like
gcc -I./ -o test main.c src_print1.c src_print2.c
You'll be good to go. There is no need for -c flag [NOTE] when you're specifying the output binary using -o.
Also, as mentioned here, all the files are in same directory, you can even shorten the statement as
gcc -o test main.c src_print1.c src_print2.c
Suggestion: While the above change(s) will do the job, this is not considered an elegant way of doing so. Please consider creating a makefile which will make your life easier.
[Note]:
Regarding the -c option, as per the online gcc manual, (emphasis mine)
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
So, it should be clear by now, why you got the error.

Segmentation fault when using CyaSSL Keygen

I'm trying to get the Keygen function of CyaSSL to work using the example in section 7.7 from here: http://www.yassl.com/yaSSL/Docs-cyassl-manual-7-keys-and-certificates.html
I'm using CyaSSL 3.2.0 with the --enable-keygen option, but couldn't get it working with 3.1.0 either.
This is the code:
#include <stdio.h>
#include <cyassl/ctaocrypt/asn.h>
#include <cyassl/ctaocrypt/rsa.h>
int main() {
RsaKey genKey;
RNG rng;
int ret;
printf("%d\n",InitRng(&rng));
printf("%d\n",InitRsaKey(&genKey, 0));
ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
printf("ret: %d\n",ret);
return 0;
}
I get a segmentation fault in the line with InitRsaKey, presumably because of an invalid write or something.
Anyone got an idea where my issue may be? Any help is appreciated
Good morning, please do not forget to include options.h header. This will ensure that you get the proper configuration settings in your project for example if you configure CyaSSL with --enable-keygen then view cyassl/options.h you will see the line #undef CYASSL_KEY_GEN followed by #define CYASSL_KEY_GEN. Also in your makefile do not forget to include the cyassl library. This can be accomplished using -lcyassl in your build line. See code below for reference:
#include <stdio.h>
#include <cyassl/options.h> //pull in the define for CYASSL_KEY_GEN
#include <cyassl/ctaocrypt/asn.h>
#include <cyassl/ctaocrypt/rsa.h>
int main() {
RsaKey genKey;
RNG rng;
int ret;
printf("%d\n",InitRng(&rng));
printf("%d\n",InitRsaKey(&genKey, 0));
ret = MakeRsaKey(&genKey, 1024, 65537, &rng);
printf("ret: %d\n",ret);
return 0;
}
Makefile:
CC=gcc #you can use clang or other instead of gcc
CFLAGS=-Wall
LIBS=-lpthread -lcyassl #must have -lcyassl in makefile
all: run
#NOTE: arrows denote a hard tab, replace them with hard tab in makefile
run: test.o
→→→→$(CC) -o $# $(LIBS) $^ $(CFLAGS) #put $(LIBS) into build command
.PHONY: clean all
clean:
→→→→rm -f *.o test.o run

How do you successfully compile a C program using the GNU Readline library?

I followed the instructions to install GNU Readline, as well as Curses, however I get some linker issues that I am unsure how to resolve. The following is my program:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <term.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char * line = readline ("Enter a line: ");
free (line);
return 0;
}
I compiled using: gcc -o main {,.c} -lreadline -lncurses (and the readline includes were where they were supposed to be, in usr/includes...
Running main gave me:
./main: symbol lookup error: /usr/local/lib/libreadline.so.6: undefined symbol: UP
Any direction as to go about resolving this would be greatly appreciated.
sudo apt-get install libreadline6-dev
gcc -o main {,.c} -lreadline -lncurses

Trouble using makefile with multiple files in C

Starting to get my head around makefiles for my C programs, but having some trouble when trying to include multiple files. Ignoring the fact that the program below is incomplete (in terms of functionality but not compilation), I'm trying to get this program compiling and running using a make file.
Here is my make file:
main: main.o IntList.o
gcc -o main main.o IntList.o
main.o: main.c
gcc -c -ansi -pedantic -Wall main.c
IntList.o: IntList.c IntList.h
gcc -c -ansi -pedantic -Wall Intlist.c
And here is the error I am receiving:
gcc -c -ansi -pedantic -Wall Intlist.c
gcc -o main main.o IntList.o
ld: duplicate symbol _getNewInt in IntList.o and main.o
collect2: ld returned 1 exit status
make: *** [main] Error 1
The code for the program is below. I'm not sure whether it's the make file or my includes in the program files that are causing problems (or both!)
Any help would be great. Cheers.
Edit: Any tips to steer me in the right direction in terms of modularization would be much appreciated as I'm not sure if I am doing this the best way.
IntList.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Constants */
#define MAX_INTS 10
/* Signed ints can have a maximum of 10 digits. We make the length 11 to
* allow for the sign in negative numbers */
#define MAX_INPUT_LENGTH 11
#define EXTRA_SPACES 2
/* Typedefs / Structs */
typedef struct {
int list[MAX_INTS];
int noInts;
} IntList;
/* Proto Types */
int insertIntToList(int *list);
void shiftList(int offset);
void displayList();
IntList.c
#include "IntList.h"
int getNewInt(int *list)
{
int valid = 0, inputInt;
char inputString[MAX_INPUT_LENGTH + EXTRA_SPACES];
while(!valid)
{
printf("Input an int: ");
valid = 1;
if((fgets(inputString, MAX_INPUT_LENGTH + EXTRA_SPACES, stdin)) != NULL)
{
sscanf(inputString, "%d", &inputInt);
/* Check first that the input string is not too long */
if(inputString[strlen(inputString) - 1] != '\n')
{
printf("\nError: Too many characters entered \n");
valid = 0;
}
printf("\nThe Int: %d", inputInt);
printf("\n");
}
}
}
void shiftList(int offset)
{
}
void displayList()
{
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "IntList.c"
int main(void)
{
int intList[10];
getNewInt(intList);
return EXIT_SUCCESS;
}
Don't include the .c file in main, include the .h file. Otherwise the code in IntList.c gets compiled both into the IntList.o and the main.o, so you'll get duplicate symbols.
Use this in main.c instead of IntList.c:
#include "IntList.h"
#include "IntList.c"
should be:
#include "IntList.h"
Also (though nothing to do with your problem) I would recommend not using mixed case in the names of source files, as it can lead to portability problems and hard to diagnose "no such file" errors - use all lower case, like the standard library headers do.
Don't #include "IntList.c" into main.c
You should not have:
#include "IntList.c"
in your main program, it should be:
#include "IntList.h"
By including the C file, you create a getNewInt in both your main and IntList object files, which is why you're getting the duplicate definition error when you try to link them together.
main.c should include "IntList.h", not "IntList.c".
If you include IntList.c, the functions in IntList.c will be implemented both in IntList.o and in main.o, which would produce the "duplicate symbol" error you're seeing.
As others have mentioned, you include .h files, not .c files
Also when you compile, you only compile .c files so you should remove any references to .h files in your Makefile

Resources