i am trying to get continuous keyboard input in c and have tried following the answer found here. however, i am getting undefined reference errors when using ncurses functions.
my code:
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
#include <ncurses.h>
typedef struct
{
int fps;
int width;
int height;
int frame;
} window;
typedef struct
{
int x;
int y;
} position;
typedef struct
{
position pos;
} snake;
int kbhit(void)
{
int ch = getch();
if (ch != ERR)
{
ungetch(ch);
return 1;
}
else
{
return 0;
}
}
void draw(window win, snake s)
{
for (int i = -1; i <= win.height; i++)
{
for (int j = -1; j <= win.width; j++)
{
if ((i == -1 || i == win.height) || (j == -1 || j == win.width))
{
printf("#");
}
else if (j == s.pos.x && i == s.pos.y)
{
printf("O");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
int main()
{
printf("Welcome to the Snake Game\n");
sleep(3);
window win = {1, 20, 10, 0};
snake s = {{19, 9}};
int key_code;
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
scrollok(stdscr, TRUE);
while (true)
{
printf("\e[1;1H\e[2J");
printf("%d\n", win.frame);
draw(win, s);
if (kbhit())
{
key_code = getch();
printf("%d", key_code);
}
usleep((int)((1.0 / win.fps) * 1000) * 1000);
win.frame++;
}
return 0;
}
output:
/usr/bin/ld: /tmp/ccZ2eIK1.o: in function `kbhit':
game.c:(.text+0xf): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x17): undefined reference to `wgetch'
/usr/bin/ld: game.c:(.text+0x2a): undefined reference to `ungetch'
/usr/bin/ld: /tmp/ccZ2eIK1.o: in function `main':
game.c:(.text+0x136): undefined reference to `initscr'
/usr/bin/ld: game.c:(.text+0x13b): undefined reference to `cbreak'
/usr/bin/ld: game.c:(.text+0x140): undefined reference to `noecho'
/usr/bin/ld: game.c:(.text+0x147): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x154): undefined reference to `nodelay'
/usr/bin/ld: game.c:(.text+0x15b): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x168): undefined reference to `scrollok'
/usr/bin/ld: game.c:(.text+0x1b6): undefined reference to `stdscr'
/usr/bin/ld: game.c:(.text+0x1be): undefined reference to `wgetch'
collect2: error: ld returned 1 exit status
You must include the NCURSES library with the link. A portable way to do this is like this:
$ gcc -o game game.c $( pkg-config --cflags --libs mcurses )
Or just include the "-lncurses" library as mentioned previously.
On an RPM-based system, you'll need the ncurses-devel package installed on the build machine.
Related
I'm using Ubuntu 22.04 and CLion to run this in C:
#include <stdio.h>
#include <sys/types.h>
#include <libusb-1.0/libusb.h>
static void print_devs(libusb_device **devs)
{
libusb_device *dev;
int i = 0;
while ((dev = devs[i++]) != NULL) {
struct libusb_device_descriptor desc;
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}
printf("%04x:%04x (bus %d, device %d)\n",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev), libusb_get_device_address(dev));
}
}
int main(void)
{
printf("hello");
libusb_device **devs;
int r;
ssize_t cnt;
r = libusb_init(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return (int) cnt;
print_devs(devs);
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}
It's a test.c for libusb. However, with libus-dev and libusb-1.0.0-dev both downloaded, I kept got this error:
====================[ Build | usbTool | Debug-System ]==========================
/snap/clion/219/bin/cmake/linux/bin/cmake --build /home/wonster/CLionProjects/usb-tool/cmake-build-debug-system --target usbTool -- -j 6
[ 50%] Building C object CMakeFiles/usbTool.dir/test.c.o
[100%] Linking C executable usbTool
/usr/bin/ld: CMakeFiles/usbTool.dir/test.c.o: in function `print_devs':
/home/wonster/CLionProjects/usb-tool/test.c:12: undefined reference to `libusb_get_device_descriptor'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:20: undefined reference to `libusb_get_device_address'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:20: undefined reference to `libusb_get_bus_number'
/usr/bin/ld: CMakeFiles/usbTool.dir/test.c.o: in function `main':
/home/wonster/CLionProjects/usb-tool/test.c:31: undefined reference to `libusb_init'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:35: undefined reference to `libusb_get_device_list'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:40: undefined reference to `libusb_free_device_list'
/usr/bin/ld: /home/wonster/CLionProjects/usb-tool/test.c:42: undefined reference to `libusb_exit'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/usbTool.dir/build.make:97: usbTool] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/usbTool.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/usbTool.dir/rule] Error 2
gmake: *** [Makefile:124: usbTool] Error 2
I just don't know the reason and how to solve this. I've followed a lot of blogs on Internet but not helpful.
I'm guessing whether there's any wrong with the CMake file:
cmake_minimum_required(VERSION 3.22)
project(usbTool C)
set(CMAKE_C_STANDARD 99)
#声明头文件路径
#set(INC_DIR /opt/homebrew/Cellar/libusb/1.0.26/include/libusb-1.0)//绝对路径
set(INC_DIR ./include)
#声明链接库路径
#set(LINK_DIR /opt/homebrew/Cellar/libusb/1.0.26/lib)//绝对路径
set(LINK_DIR ./lib)
#引入头文件
include_directories(${INC_DIR})
#引入库文件
link_directories(${LINK_DIR})
add_executable(usbTool test.c)
#引入第三方库
target_link_libraries(usbTool libusb-1.0.a)
And this is the project content:
Any help is remarkable! Thanks in advance!
I'm trying to compile a small proof of work tath run lua code on the rasperry pi zero bare-metal.
All my object file and lua's have been compiled with similar options. I tried with and without the -nostdlib and -lc -lgcc but the output is the same.
Commande:
arm-none-eabi-gcc \
-mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s \
-g -O0 \
-Wl,-z,max-page-size=0x04,-T,linker.ld \
-nostdlib \
./lua/lua-5.4.4/src/lapi.o ./lua/lua-5.4.4/src/lauxlib.o ./lua/lua-5.4.4/src/lbaselib.o ./lua/lua-5.4.4/src/lcode.o ./lua/lua-5.4.4/src/lcorolib.o ./lua/lua-5.4.4/src/lctype.o ./lua/lua-5.4.4/src/ldblib.o ./lua/lua-5.4.4/src/ldebug.o ./lua/lua-5.4.4/src/ldo.o ./lua/lua-5.4.4/src/ldump.o ./lua/lua-5.4.4/src/lfunc.o ./lua/lua-5.4.4/src/lgc.o ./lua/lua-5.4.4/src/linit.o ./lua/lua-5.4.4/src/liolib.o ./lua/lua-5.4.4/src/llex.o ./lua/lua-5.4.4/src/lmathlib.o ./lua/lua-5.4.4/src/lmem.o ./lua/lua-5.4.4/src/loadlib.o ./lua/lua-5.4.4/src/lobject.o ./lua/lua-5.4.4/src/lopcodes.o ./lua/lua-5.4.4/src/loslib.o ./lua/lua-5.4.4/src/lparser.o ./lua/lua-5.4.4/src/lstate.o ./lua/lua-5.4.4/src/lstring.o ./lua/lua-5.4.4/src/lstrlib.o ./lua/lua-5.4.4/src/ltable.o ./lua/lua-5.4.4/src/ltablib.o ./lua/lua-5.4.4/src/ltm.o ./lua/lua-5.4.4/src/lundump.o ./lua/lua-5.4.4/src/lutf8lib.o ./lua/lua-5.4.4/src/lvm.o ./lua/lua-5.4.4/src/lzio.o \
./.build/startup.o ./.build/start.o ./.build/main.o ./.build/bootstrap.lua.o ./.build/0stubs.o \
-lc \
-lgcc \
-lm
Output:
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-fstatr.o): in function `_fstat_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/fstatr.c:55: undefined reference to `_fstat'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-openr.o): in function `_open_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/openr.c:50: undefined reference to `_open'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-timesr.o): in function `_times_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/timesr.c:52: undefined reference to `_times'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-unlinkr.o): in function `_unlink_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/unlinkr.c:47: undefined reference to `_unlink'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-writer.o): in function `_write_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/writer.c:49: undefined reference to `_write'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-closer.o): in function `_close_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/closer.c:47: undefined reference to `_close'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-gettimeofdayr.o): in function `_gettimeofday_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/gettimeofdayr.c:62: undefined reference to `_gettimeofday'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-isattyr.o): in function `_isatty_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/isattyr.c:52: undefined reference to `_isatty'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-linkr.o): in function `_link_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/linkr.c:53: undefined reference to `_link'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-lseekr.o): in function `_lseek_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/lseekr.c:49: undefined reference to `_lseek'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-readr.o): in function `_read_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/readr.c:49: undefined reference to `_read'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-signalr.o): in function `_kill_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:53: undefined reference to `_kill'
/usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/bin/ld: /usr/lib/gcc/arm-none-eabi/9.2.1/../../../arm-none-eabi/lib/arm/v5te/hard/libc.a(lib_a-signalr.o): in function `_getpid_r':
/build/newlib-wFsRXh/newlib-3.3.0/build/arm-none-eabi/arm/v5te/hard/newlib/libc/reent/../../../../../../../../newlib/libc/reent/signalr.c:83: undefined reference to `_getpid'
My 0stubs.c file:
#include <sys/stat.h>
/* A helper function written in assembler to aid us in allocating memory */
extern caddr_t _get_stack_pointer(void);
/* Increase program data space. As malloc and related functions depend on this,
it is useful to have a working implementation. The following suffices for a
standalone system; it exploits the symbol _end automatically defined by the
GNU linker. */
caddr_t _sbrk( int incr )
{
extern char _end;
static char* heap_end = 0;
char* prev_heap_end;
if( heap_end == 0 )
heap_end = &_end;
prev_heap_end = heap_end;
heap_end += incr;
return (caddr_t)prev_heap_end;
}
int close(int file) {
return -1;
}
char *__env[1] = { 0 };
char **environ = __env;
#include <errno.h>
#undef errno
extern int errno;
int execve(char *name, char **argv, char **env) {
errno = ENOMEM;
return -1;
}
int fork(void) {
errno = EAGAIN;
return -1;
}
int kill(int pid, int sig) {
errno = EINVAL;
return -1;
}
int link(char *old, char *new) {
errno = EMLINK;
return -1;
}
int fstat(int file, struct stat *st) {
st->st_mode = S_IFCHR;
return 0;
}
int getpid(void) {
return 1;
}
int isatty(int file) {
return 1;
}
int lseek(int file, int ptr, int dir) {
return 0;
}
int open(const char *name, int flags, int mode) {
return -1;
}
int read(int file, char *ptr, int len) {
return 0;
}
int times(struct tms *buf) {
return -1;
}
int unlink(char *name) {
errno = ENOENT;
return -1;
}
int wait(int *status) {
errno = ECHILD;
return -1;
}
int write(int file, char *ptr, int len) {
int todo;
for (todo = 0; todo < len; todo++) {
//outbyte (*ptr++);
}
return len;
}
void abort(void){
while(1){}
}
void exit(int status){
while(1){}
}
I have a C program. I want to run it on Linux. But, when I am entering gcc seekingtutor.c -o seek I am getting error. And, sometimes its showing file not exists on the directory. Is there any way I can run this program?
Code is here in GitHub
I want to run the following code on linux terminal. I am seeking for the command it needs to run. I think pthread compiling is needed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <assert.h>
//maximum student numbers
#define Max_stu_size 2000
//const int Max_stu_size=2000;
//check how many students have done.
//then coordinate and tutors threads can terminate
int done=0;
int totalRequests=0;
int totalSessions=0;
int tutoringNow=0;
void *student_thread(void *student_id);
void *tutor_thread(void *tutor_id);
void *coordinator_thread();
//void *coordinator_thread(void *);
int student_num=0;
int tutor_num=0;
int help_num=0;
int chair_num=0;
int occupied_chair_num=0;
int newArrivedStudentQueue[Max_stu_size];
int tutorFinishedQueue[Max_stu_size];
int priorityQueue[Max_stu_size][2];
int student_priority[Max_stu_size];
int student_ids[Max_stu_size];
int tutor_ids[Max_stu_size];
sem_t sem_student;
sem_t sem_coordinator;
//sem_t sem_tutorToStudent[Max_stu_size];
pthread_mutex_t seatLock;
pthread_mutex_t queueLock;
pthread_mutex_t tutorFinishedQueueLock;
int main(int argc, const char * argv[]) {
//test
/*student_num=10;
tutor_num=3;
chair_num=4;
help_num=5;*/
//get input argument
if (argc != 5){
printf("Usage: <# of Students> <# of tutors> <# of chairs> <# of help>\n");
exit(-1);
}
student_num=atoi(argv[1]);
tutor_num=atoi(argv[2]);
chair_num=atoi(argv[3]);
help_num=atoi(argv[4]);
if(student_num > Max_stu_size || tutor_num > Max_stu_size){
printf("Max student number is: %d; Max tutor number is: %d\n", Max_stu_size, Max_stu_size);
exit(-1);
}
//init arrays
int i;
for(i=0;i<student_num;i++){
newArrivedStudentQueue[i]=-1;
tutorFinishedQueue[i]=-1;
priorityQueue[i][0]=-1;
priorityQueue[i][1]=-1;
student_priority[i]=0;
}
//init lock and semaphore
sem_init(&sem_student,0,0);
sem_init(&sem_coordinator,0,0);
pthread_mutex_init(&seatLock,NULL);
pthread_mutex_init(&queueLock,NULL);
pthread_mutex_init(&tutorFinishedQueueLock,NULL);
/*for(i=0;i<student_num;i++){
sem_init(&sem_tutorToStudent[i],0,0);
}*/
//allocate threads
pthread_t students[student_num];
pthread_t tutors[tutor_num];
pthread_t coordinator;
//create threads
assert(pthread_create(&coordinator,NULL,coordinator_thread,NULL)==0);
for(i = 0; i < student_num; i++)
{
student_ids[i] = i + 1;
assert(pthread_create(&students[i], NULL, student_thread, (void*) &student_ids[i])==0);
}
for(i = 0; i < tutor_num; i++)
{
tutor_ids[i] = i + student_num + 1;
assert(pthread_create(&tutors[i], NULL, tutor_thread, (void*) &tutor_ids[i])==0);
}
//join threads
pthread_join(coordinator, NULL);
for(i =0; i < student_num; i++)
{
pthread_join(students[i],NULL);
}
for(i =0; i < tutor_num; i++)
{
pthread_join(tutors[i],NULL);
}
return 0;
}
//students action.
//1.program;
//2.find the empty seat and nofity coordinator;
//3.wait tutor to wake it up and finish tutoring.
void *student_thread(void *student_id){
int id_student=*(int*)student_id;
while(1){
if(student_priority[id_student-1]>=help_num) {
pthread_mutex_lock(&seatLock);
done++;
pthread_mutex_unlock(&seatLock);
//notify coordinate to terminate
sem_post(&sem_student);
//printf("------student %d terminate------\n",id_student);
pthread_exit(NULL);
}
//programing
//sleeping for a random amount of time up to 2 ms
float programTime=(float)(rand()%200)/100;
//printf("sleep time = %f.\n", programTime);
usleep(programTime);
pthread_mutex_lock(&seatLock);
if(occupied_chair_num>=chair_num){
//St: Student x found no empty chair. Will try again later
printf("St: Student %d found no empty chair. Will try again later.\n",id_student);
pthread_mutex_unlock(&seatLock);
continue;
}
occupied_chair_num++;
totalRequests++;
newArrivedStudentQueue[id_student-1]=totalRequests;
//St: Student x takes a seat. Empty chairs = <# of empty chairs>.
printf("St: Student %d takes a seat. Empty chairs = %d\n",id_student,chair_num-occupied_chair_num);
pthread_mutex_unlock(&seatLock);
//notify coordinator that student seated.
sem_post(&sem_student);
//wait to be tutored.
while(tutorFinishedQueue[id_student-1]==-1);
//sem_wait(&sem_tutorToStudent[id_student-1]);
//St: Student x received help from Tutor y.
printf("St: Student %d received help from Tutor %d.\n",id_student,tutorFinishedQueue[id_student-1]-student_num);
//reset the shared data
pthread_mutex_lock(&tutorFinishedQueueLock);
tutorFinishedQueue[id_student-1]=-1;
pthread_mutex_unlock(&tutorFinishedQueueLock);
//tutored times++ after tutoring.
pthread_mutex_lock(&seatLock);
student_priority[id_student-1]++;
pthread_mutex_unlock(&seatLock);
}
}
//tutors action
//1.wait coordinator's nofitication;
//2.find student from queue with highest priority;
//3.tutoring and notify student it's done;
void *tutor_thread(void *tutor_id){
int id_tutor=*(int*)tutor_id;
int studentTutoredTimes;
//students with same tutored times, who come first has higher priority
int studentSequence;
int id_student;
while(1){
//if all students finish, tutors threads terminate.
if(done==student_num){
//printf("------tutor %d terminate------\n",id_tutor);
pthread_exit(NULL);
}
studentTutoredTimes=help_num-1;
studentSequence=student_num*help_num+1;
id_student=-1;
//wait coordinator's notification
sem_wait(&sem_coordinator);
pthread_mutex_lock(&seatLock);
//find student with highest priority from priority queue;
//if students with same tutored times, who come first has higher priority
int i;
for(i=0;i<student_num;i++){
if(priorityQueue[i][0]>-1 && priorityQueue[i][0]<=studentTutoredTimes
&& priorityQueue[i][1]<studentSequence){
studentTutoredTimes=priorityQueue[i][0];
studentSequence=priorityQueue[i][1];
id_student=student_ids[i];
}
}
//in case no student in the queue.
if(id_student==-1) {
pthread_mutex_unlock(&seatLock);
continue;
}
//pop the student(reset the priority queue)
priorityQueue[id_student-1][0]=-1;
priorityQueue[id_student-1][1]=-1;
//occupied chair--
occupied_chair_num--;
//all the students who are receiving tutoring now, since each tutor time slice is very tiny, so it's common that the tutoringNow is 0.
tutoringNow++;
pthread_mutex_unlock(&seatLock);
//tutoring
// sleeping for a random amount of time up to 0.2 ms
float tutorTime=(float)(rand()%200)/1000;
usleep(tutorTime);
//after tutoring
pthread_mutex_lock(&seatLock);
//need to do tutoringNow-- after tutoring.
tutoringNow--;
totalSessions++;
printf("Tu: Student %d tutored by Tutor %d. Students tutored now = %d. Total sessions tutored = %d\n",id_student,id_tutor-student_num,tutoringNow,totalSessions);
pthread_mutex_unlock(&seatLock);
//update shared data so student can know who tutored him.
pthread_mutex_lock(&tutorFinishedQueueLock);
tutorFinishedQueue[id_student-1]=id_tutor;
pthread_mutex_unlock(&tutorFinishedQueueLock);
//wake up the tutored student.
//sem_post(&sem_tutorToStudent[id_student-1]);
}
}
//coordinator action.
//1.wait student's nofitication;
//2.push student into the queue with priority;
//3.notify tutor
void *coordinator_thread(){
while(1){
//if all students finish, tutors threads and coordinate thread terminate.
if(done==student_num){
//terminate tutors first
int i;
for(i=0;i<tutor_num;i++){
//notify tutors to terminate
sem_post(&sem_coordinator);
}
//terminate coordinate itself
//printf("------coordinator terminate------\n");
pthread_exit(NULL);
}
//wait student's notification
sem_wait(&sem_student);
pthread_mutex_lock(&seatLock);
//find the students who just seated and push them into the priority queue
int i;
for(i=0;i<student_num;i++){
if(newArrivedStudentQueue[i]>-1){
priorityQueue[i][0]=student_priority[i];
priorityQueue[i][1]=newArrivedStudentQueue[i];
printf("Co: Student %d with priority %d in the queue. Waiting students now = %d. Total requests = %d\n",student_ids[i],student_priority[i],occupied_chair_num,totalRequests);
newArrivedStudentQueue[i]=-1;
//notify tutor that student is in the queue.
sem_post(&sem_coordinator);
}
}
pthread_mutex_unlock(&seatLock);
}
}
Error:
gcc seekingtutor.c -o seek
/usr/bin/ld: /tmp/ccsL8SkI.o: in function `main':
seekingtutor.c:(.text+0x1a2): undefined reference to `sem_init'
/usr/bin/ld: seekingtutor.c:(.text+0x1b8): undefined reference to `sem_init'
/usr/bin/ld: seekingtutor.c:(.text+0x38e): undefined reference to `pthread_create'
/usr/bin/ld: seekingtutor.c:(.text+0x41c): undefined reference to `pthread_create'
/usr/bin/ld: seekingtutor.c:(.text+0x4c5): undefined reference to `pthread_create'
/usr/bin/ld: seekingtutor.c:(.text+0x50c): undefined reference to `pthread_join'
/usr/bin/ld: seekingtutor.c:(.text+0x530): undefined reference to `pthread_join'
/usr/bin/ld: seekingtutor.c:(.text+0x563): undefined reference to `pthread_join'
/usr/bin/ld: /tmp/ccsL8SkI.o: in function `student_thread':
seekingtutor.c:(.text+0x60d): undefined reference to `sem_post'
/usr/bin/ld: seekingtutor.c:(.text+0x722): undefined reference to `sem_post'
/usr/bin/ld: /tmp/ccsL8SkI.o: in function `tutor_thread':
seekingtutor.c:(.text+0x866): undefined reference to `sem_wait'
/usr/bin/ld: /tmp/ccsL8SkI.o: in function `coordinator_thread':
seekingtutor.c:(.text+0xad7): undefined reference to `sem_post'
/usr/bin/ld: seekingtutor.c:(.text+0xafc): undefined reference to `sem_wait'
/usr/bin/ld: seekingtutor.c:(.text+0xc08): undefined reference to `sem_post'
collect2: error: ld returned 1 exit status
$ ./seekingtutor
bash: ./seekingtutor: No such file or directory
While executing, you need to run it using ./seek, not ./seekingtutor as -o seek outputs the executable with the name seek
The linker has shown errors collect2: error: ld returned 1 exit status, so it did not output the executable.
It seems like it needs pthread. So run gcc seekingtutor.c -o seek -lpthread to compile it, and try. It works for me here.
https://man7.org/linux/man-pages/man3/sem_init.3.html says
Link with -pthread.
I have a cryptic error, and a warning. The warning: integer initialized to pointer on assignment the other is symbol not found, referring to function popdepholder.
I believe they may somehow be related due to a phantom function that exists but can't be seen or something
header dependency.h:
#include "relation.h"
#include "strhelp.h"
#include <time.h>
#ifndef _DEPENDENCY_H
#define _DEPENDENCY_H
typedef struct fd {
RELATION *left;
RELATION *right;
RELATION *referring; } DEPENDENCY;
typedef struct dh {
DEPENDENCY *data;
struct dh *next;
struct dh *prev; } DEP_HOLDER;
DEP_HOLDER * popdepholer(DEP_HOLDER **top);
...
void removedepholderat(DEP_HOLDER **list,int dest);
...
DEP_HOLDER * popdepholer(DEP_HOLDER **top) ###this function is not being seen
{
DEP_HOLDER * remove = 0;
if(*top)
{
remove=(*top);
if(remove->prev)
{
if(remove->next) /*the middle case*/
{
remove->prev->next=remove->next;
remove->next->prev=remove->prev;
(*top)=(*top)->next;
}
else /*the bottom case*/
{
remove->prev->next=0;
*top=0;
}
}
else if((remove->next)&&!(remove->prev)) /*the top case*/
{
remove->next->prev=0;
(*top)=(*top)->next;
}
else /*the single case*/
{
*top = 0;
}
remove->prev=0;
remove->next=0;
}
else
{
printf("In popdepholder, passed a null pointer!\n");
exit(4);
}
return remove;
}
...
void removedepholderat(DEP_HOLDER **list,int dest)
{
DEP_HOLDER *holder = *list;
if(dest == 1) #here
{
DEP_HOLDER * r = popdepholder(list); #here
destroydepholder(&r);
}
else
{
while((holder) && (dest>1))
{
holder=holder->next;
dest--;
}
if((holder) && dest==1)
{
DEP_HOLDER * r = popdepholder(&holder); #here
destroydepholder(&r);
}
}
}
#endif
main.c:
#include "dependency.h"
DEP_HOLDER *mydp = getnewholder(d1);
...
DEP_HOLDER * a = popdepholder(&mydp);
error:
In file included from test-dependency.c:3:
dependency.h: In function ‘removedepholderat’:
dependency.h:332: warning: initialization makes pointer from integer without a cast
dependency.h:344: warning: initialization makes pointer from integer without a cast
test-dependency.c: In function ‘main’:
test-dependency.c:37: warning: initialization makes pointer from integer without a cast
Undefined symbols for architecture x86_64:
"_popdepholder", referenced from:
_removedepholderat in ccUGYlgF.o
_main in ccUGYlgF.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
You've declared and defined popdepholer instead of popdepholder (missing the second d).
Perhaps you are attempting to link a 32-bit library into a 64-bit binary. Check that your makefile build process is linking in the correct library.
I wanted to compare AES algorithm performance from libtomcrypt in Windows and Ubuntu by creating a benchmark-like file, but I have got errors while coding it. Please help me. Below is my file for comparing:
Compare.c:
`#include <time.h> `
#include <tomcrypt.h>
#define MIN_TIME 10.0
#define MIN_ITERS 20 `
double test_rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) {
int iterations = 0;
clock_t start;
double elapsed=0.0;
int out;
start=clock();
do{
out = rijndael_ecb_encrypt(pt, ct, skey);
iterations++;
elapsed=(clock()-start)/(double)CLOCKS_PER_SEC;
} while(elapsed<MIN_TIME || iterations<MIN_ITERS);
elapsed=1000.0*elapsed/iterations;
printf("%s \n",pt);
printf("%s \n",skey->data);
printf("%s \n",ct);
printf("iterations: %8d \n",iterations);
printf("%8.2lf ms per iteration \n",elapsed);
printf("out: %d \n",out);
return elapsed;
}
int main() {
unsigned char pt[22]="-K4)<i50~'APg2fa7DiV";
unsigned char ct[22];
unsigned char key[16]="EDB1C6D13FC72";
symmetric_key *skey;
int err;
double tout1;
printf("%x",sizeof(pt));
printf("%l",sizeof(key));
if((err=rijndael_setup(key,16,0,skey))!=CRYPT_OK) {
printf("%s",error_to_string(err));
return -1;
}
tout1=test_rijndael_ecb_encrypt(pt,ct,skey);
printf("%s \n",ct);
printf("%f",tout1);
return 0;
}
But when I compile this it shows runtime errors as:
gcc -o "TestC" ./src/TestC.o
./src/TestC.o: In function `test_rijndael_ecb_encrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:27: undefined reference to `rijndael_ecb_encrypt'
./src/TestC.o: In function `test_rijndael_ecb_decrypt':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:53: undefined reference to `rijndael_ecb_decrypt'
./src/TestC.o: In function `main':
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:82: undefined reference to `rijndael_setup'
/home/anvesh/workspace/TestC/Debug/../src/TestC.c:83: undefined reference to `error_to_string'
collect2: error: ld returned 1 exit status
make: *** [TestC] Error 1
Where did I go wrong?
You forgot to link with tomcrypt library. Compile with -ltomcrypt to link the library:
gcc file.c -ltomcrypt