This following is an OS simulator called USLOSS. The main.c is executed in the phase1.a library, and this is the 4th phase of the project. Below is generated from a call to make test00
gcc -Wall -g -I./usloss/include -D_XOPEN_SOURCE -D_XOPEN_SOURCE -L. -L./usloss/lib testcases/test00.c -o testcases/test00
testcases/test00.c:18:5: warning: implicit declaration of function
'GetTimeofDay' is invalid in C99 [-Wimplicit-function-declaration]
GetTimeofDay(&begin);
^
testcases/test00.c:30:5: warning: implicit declaration of function 'Terminate'
is invalid in C99 [-Wimplicit-function-declaration]
Terminate(1);
^
testcases/test00.c:44:5: warning: implicit declaration of function 'Spawn' is
invalid in C99 [-Wimplicit-function-declaration]
Spawn("Child1", Child, "1", USLOSS_MIN_STACK, 4, &pid);
^
testcases/test00.c:49:5: warning: implicit declaration of function 'Wait' is
invalid in C99 [-Wimplicit-function-declaration]
Wait(&pid, &status);
^
4 warnings generated.
Undefined symbols for architecture x86_64:
"_GetTimeofDay", referenced from:
_Child in test00-4284b8.o
"_Sleep", referenced from:
_Child in test00-4284b8.o
"_Spawn", referenced from:
_start4 in test00-4284b8.o
"_Terminate", referenced from:
_Child in test00-4284b8.o
_start4 in test00-4284b8.o
"_USLOSS_Console", referenced from:
_Child in test00-4284b8.o
_start4 in test00-4284b8.o
"_Wait", referenced from:
_start4 in test00-4284b8.o
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [testcases/test00] Error 1
make test00 should create an executable version of the test00.c file, but I don't understand why the program is failing to link. Supplied is my Makefile
TARGET = libphase4.a
ASSIGNMENT = 452phase1
CC = gcc
AR = ar
COBJS = phase4.o libuser.o
CSRCS = ${COBJS:.o=.c}
#PHASE1LIB = phase1
#PHASE2LIB = phase2
#PHASE3LIB = phase3
PHASE1LIB = phase1debug
PHASE2LIB = phase2debug
PHASE3LIB = phase3debug
HDRS = phase1.h phase2.h phase3.h phase4.h libuser.h provided_prototypes.h
INCLUDE = ./usloss/include
CFLAGS = -Wall -g -I${INCLUDE}
CFLAGS += -D_XOPEN_SOURCE
UNAME := $(shell uname -s)
ifeq ($(UNAME), Darwin)
CFLAGS += -D_XOPEN_SOURCE # use for Mac, NOT for Linux!!
endif
LDFLAGS = -L. -L./usloss/lib
PHASE4 = /home/cs452/fall15/phase4
ifeq ($(PHASE4), $(wildcard $(PHASE4)))
LDFLAGS += -L$(PHASE4)
endif
TESTDIR = testcases
TESTS = test00 test01 test02 test03 test04 test05 test06 test07 test08 \
test09 test10 test11 test12 test13 test14 test15 test16 test17 \
test18 test19 test20 test21 test22
LIBS = -lusloss -l$(PHASE1LIB) -l$(PHASE2LIB) -l$(PHASE3LIB) -lphase4
$(TARGET): $(COBJS)
$(AR) -r $# $(COBJS)
#$(TESTS): $(TARGET) $(TESTDIR)/$$#.c
$(TESTS): $(TARGET)
$(CC) $(CFLAGS) -I. -c $(TESTDIR)/$#.c
$(CC) $(LDFLAGS) -o $# $#.o $(LIBS)
clean:
rm -f $(COBJS) $(TARGET) test??.o test?? core term*.out
phase4.o: phase4.c libuser.c
submit: $(CSRCS) $(HDRS) Makefile
tar cvzf phase4.tgz $(CSRCS) $(HDRS) Makefile
I don't understand Makefiles well enough to understand what is happening with the linker. Also supplied is the test00.c
#include <stdlib.h>
#include <stdio.h>
#include <usloss.h>
#include "phase1.h"
#include "phase2.h"
#include <usyscall.h>
#include "libuser.h"
#include <assert.h>
#define ABS(a,b) a-b > 0 ? a-b : -(a-b)
int Child(char *arg)
{
int begin, end, time;
int me = atoi(arg);
USLOSS_Console("Child%d(): Going to sleep for 10 seconds\n", me);
GetTimeofDay(&begin);
Sleep(10);
GetTimeofDay(&end);
time = end - begin;
time = ABS(10000000, time);
if (time > 1000000) {
USLOSS_Console("Child%d(): Sleep bad: %d %d\n",
me, time, ABS(10000000, time));
}
else {
USLOSS_Console("Child%d(): Sleep done at time %d\n", me, end);
}
Terminate(1);
return 0;
} /* Child */
int start4(char *arg)
{
int pid, status;
USLOSS_Console("start4(): Start 5 children who all sleep for 10 seconds. Upon\n");
USLOSS_Console(" waking up, each child checks if its sleep time was at\n");
USLOSS_Console(" least 10 seconds.\n");
Spawn("Child1", Child, "1", USLOSS_MIN_STACK, 4, &pid);
Spawn("Child2", Child, "2", USLOSS_MIN_STACK, 4, &pid);
Spawn("Child3", Child, "3", USLOSS_MIN_STACK, 4, &pid);
Spawn("Child4", Child, "4", USLOSS_MIN_STACK, 4, &pid);
Spawn("Child5", Child, "5", USLOSS_MIN_STACK, 4, &pid);
Wait(&pid, &status);
Wait(&pid, &status);
Wait(&pid, &status);
Wait(&pid, &status);
Wait(&pid, &status);
USLOSS_Console("start4(): Test sleep done.\n");
Terminate(0);
return 0;
}
Why does this linker error happen and what can I do to fix it and test my files? The result should be an executable .o file to run and see the output.
I'm in your class. It looks like you need to use the MacOSX libraries.
Related
This is the error when I run make:
Undefined symbols for architecture arm64:
"\_add", referenced from:
\_main in a4-1-989722.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: \*\*\* \[a4-1\] Error 1
note (one of the functions in my module is add()
This is the makefile:
CC = gcc
CFLAGS = -Wall -Wextra -pedantic
TARGET = a4-1
DEP1 = myMath
all: $(TARGET)
debug: CFLAGS += -DDBUG
debug: $(TARGET)
&(TARGET): $(TARGET).c $(DEP1).o
$(CC) $(CFLAGS) $(DEP1).o -o $(TARGET) $(TARGET).c
$(DEP1).o: $(DEP1).c
$(CC) $(CFLAGS) -c $(DEP1).c
clean:
rm $(TARGET) *.o
This is the module .c file:
#include <stdio.h>
#include <stdlib.h>
int add(int x, int y)
{
return(x + y);
}
This is the module .h file:
int add(int, int);
#include "module.h"
in module.c
I am trying to compile two c files into one executable. In the directory I have only three files; Makefile, main.c and myfunction.c.
Makefile:
CC = gcc
CFLAGS = -Wall -g -O0
LIBS = -lm
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
MAIN = main
all: $(MAIN)
#echo Program has been compiled
$(MAIN): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LIBS)
clean:
$(RM) *.o *~ $(MAIN)
main.c:
#include <stdio.h>
void myfunc();
int main (int argc, char *argv[]) {
myfunc();
return 0;
}
myfunction.c:
#include <stdio.h>
void myfunc() { printf("hello world"); }
output after make:
gcc -Wall -g -O0 -c -o main.o main.c
gcc -Wall -g -O0 -c -o myfunction.o myfunction.c
gcc -Wall -g -O0 -o main main.o myfunction.o -lm
Undefined symbols for architecture x86_64:
"_myfunc", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1
I had something nearly identical working in the past. I have since clean installed MacOS and updated to Big Sur. Is this the issue or have I overlooked something?
I fixed the issue. I’m not sure what part fixed it, but installed Homebrew and used it to install gcc-10. I also deleted the project and started over.
myfunc would define like file header
myfunc.h
void myfunc()
Declare in another file
myfunc.c
void myfunc() { printf("hello world"); }
Follow the following tutorial
https://developer.gnome.org/anjuta-build-tutorial/stable/build-make.html.en
I'm trying to use powf function in an rtems application.
When I call powf(a,b); inside Init() function, it compiles ok.
But when I call powf in some other function, the compiler gives me 'undefined reference to powf' message even though I have those #include <math.h> and #include <float.h>. I event tried merging the file, but it is the same.
#define CONFIGURE_...
#define CONFIGURE_...
#include <rtems/confdefs.h>
rtems_task Init( rtems_task_argument ignored)
{
powf(a,b); // ok
}
int my_other_func()
{
powf(c,d); // undefined reference error..
}
What can be the problem?
EDIT(ADD) : I added source code and makefile below. The compiled rtems OS package is specified by shell environment variable RTEMS_MAKEFILE_PATH.
Makefile :
include ../Makefile.base
_RAM_START = 0x60000000
XCFLAGS = -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=$(_RAM_START)
XCFLAGS += -lm -DALDEBARAN_RTEMS
../Makefile.base :
#
# RTEMS_MAKEFILE_PATH is typically set in an environment variable
#
PGM=${ARCH}/faster_rcnn.exe
# optional managers required
MANAGERS=all
# C source names
VPATH = ../src
VPATH += ../../../../abfrcnn/bare-c/lrn_layer
CSRCS = init.c
CSRCS += lrn_layer.c
CSRCS1 = $(notdir $(CSRCS))
COBJS_ = $(CSRCS1:.c=.o)
include $(RTEMS_MAKEFILE_PATH)/Makefile.inc
include $(RTEMS_CUSTOM)
include $(PROJECT_ROOT)/make/leaf.cfg
#XCFLAGS += -I../../include
COBJS = $(COBJS_:%=${ARCH}/%)
OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS)
#all: ${ARCH} $(PGM) RUNTCL
all: ${ARCH} $(PGM)
$(PGM): $(OBJS)
$(make-exe)
RUNTCL:
echo 'system_init' > run.tcl
echo 'load_image o-optimize/faster_rcnn.exe' >> run.tcl
echo 'run $(_RAM_START)' >> run.tcl
clean:
-$(RM) -r $(ARCH)
../src/init.c :
...
#include <math.h>
rtems_status Init(rtems_argument ignored)
{
...
printf(" pow(1.1,2.2) = %f\n", powf(1.1,2.2)); // <== powf compiles liks ok
//zf_coco();
}
../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c
#include <stdio.h>
#include <math.h>
int lrn_layer(... args... )
{
...
val = 1./powf((1.+a/(float)(k^2)*tmp),b); // method1
...
} // main ROI loop
The result of make command :
test -d o-optimize || mkdir o-optimize
sparc-ab-rtems-gcc --pipe -B/home/ckim/prj/abts/rtems-qt/rtems-4.10.99-kernel/build-rtems/rtems-package/sparc-ab-rtems/aldebaran2/lib/ -specs bsp_specs -qrtems -Wall -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=0x60000000 -lm -DALDEBARAN_RTEMS -O4 -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN -c -o o-optimize/init.o ../src/init.c
../src/init.c:120:2: warning: missing braces around initializer [-Wmissing-braces]
{0}, // rtems_chain_control;
^
../src/init.c:120:2: warning: (near initialization for 'ald_sd_card_driver_table[0].queue.Chain') [-Wmissing-braces]
sparc-ab-rtems-gcc --pipe -B/home/ckim/prj/abts/rtems-qt/rtems-4.10.99-kernel/build-rtems/rtems-package/sparc-ab-rtems/aldebaran2/lib/ -specs bsp_specs -qrtems -Wall -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=0x60000000 -lm -DALDEBARAN_RTEMS -O4 -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN -c -o o-optimize/lrn_layer.o ../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c
../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c: In function 'lrn_layer':
../../../../abfrcnn/bare-c/lrn_layer/lrn_layer.c:201:12: warning: 'w_idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
if (w_idx == w && h_idx == h) {
^
sparc-ab-rtems-gcc --pipe -B/home/ckim/prj/abts/rtems-qt/rtems-4.10.99-kernel/build-rtems/rtems-package/sparc-ab-rtems/aldebaran2/lib/ -specs bsp_specs -qrtems -Wall -qnolinkcmds -T ../../lib/linkcmds.abts3 -D_RAM_START=0x60000000 -lm -DALDEBARAN_RTEMS -O4 -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN -L/opt/abde-rtems/lib/gcc/sparc-ab-rtems/4.8.2/soft -L/opt/abde-rtems/sparc-ab-rtems/lib/soft -mtune=v8 -msoft-float -fcommon -DTARGET_ALDEBARAN -o o-optimize/faster_rcnn.exe o-optimize/init.o o-optimize/lrn_layer.o
o-optimize/lrn_layer.o: In function `lrn_layer':
lrn_layer.c:(.text+0x4a8): undefined reference to `powf'
collect2: error: ld returned 1 exit status
make: *** [o-optimize/faster_rcnn.exe] Error 1
The compile process is composed of compilation of each individual .c files and final linking. I found somehow the -lm option which was added through XCFLAGS is being applied to the compilation command but not in the linking command. So I added -lm option in the object list so that that option is naturally following the object list in the link command. (I guess the proper way is to add -lm to the XLDFLAG because I found the variable in rtems build tree : extra LD flags). I'll try later..
OBJS= $(COBJS) $(CXXOBJS) $(ASOBJS) -lm
I a ma total noob in C programming. I took some code that throws this error when I run make :
Undefined symbols for architecture x86_64:
"_rp_osc_adc_sign", referenced from:
_rp_osc_meas_min_max in worker.o
_meas_period in worker.o
"_rp_osc_meas_cnv_cnt", referenced from:
_rp_osc_meas_convert in worker.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [make_c_app] Error
I am using OS X Yosemite and here's the make file
CC=$(CROSS_COMPILE)gcc
RM=rm
OBJECTS=main.o fpga.o worker.o calib.o fpga_awg.o generate.o fpga_pid.o pid.o
INCLUDE=
#CFLAGS=$(CFLAGS) -Wall -Werror -g
CFLAGS+= -Wall -Werror -g -fPIC $(INCLUDE)
LDFLAGS=-shared
OUT_DIR=../
C_OUT_NAME=$(OUT_DIR)controller.so
all: make_c_app
clean: clean_c_app
make_c_app: $(OBJECTS)
$(CC) -o $(C_OUT_NAME) $(OBJECTS) $(CFLAGS) $(LDFLAGS)
clean_c_app:
$(RM) -f $(C_OUT_NAME) $(OBJECTS)
and the method the linker is complaining about
int rp_osc_meas_min_max(rp_osc_meas_res_t *ch_meas, int sig_data)
{
int s_data = rp_osc_adc_sign(sig_data);
if(ch_meas->min > s_data)
ch_meas->min = s_data;
if(ch_meas->max < s_data)
ch_meas->max = s_data;
ch_meas->avg += s_data;
return 0;
}
Where the error can come from ?
EDIT : rp_osc_adc_sign is defined the following way
inline int rp_osc_adc_sign(int in_data)
{
int s_data = in_data;
if(s_data & (1<<(c_osc_fpga_adc_bits-1)))
s_data = -1 * ((s_data ^ ((1<<c_osc_fpga_adc_bits)-1)) + 1);
return s_data;
}
You should delete the inline tag that is written before the function
rp_osc_adc_sign(int in_data)
rp_osc_meas_cnv_cnt
For some reason I have having an issue compiling a shared object that uses ncurses. Even though I include and link with -lncurses, compiling the .so file fails. Please advise.
#include <string.h>
#include "../include/mod_curse.h" /* Includes ncurses.h and friends */
int OnConsoleCmd(McapiS *API, ArgS *CmdArgs) /* Just ignore these, they're included in mod_curse.h */
{
if(!strcmp(CmdArgs->Data, "help"))
{
API->BPrintf(STD, "\n-- mod_curse.so --\n");
return 0;
}
}
int OnLoad(McapiS *API, va_list Args)
{
initscr(); /* These are the problems */
}
/* Time to clean up and unload the module */
int OnDeload(McapiS *API, va_list Args)
{
endwin();
}
Here is the Makefile:
CC = clang
CFLAGS = -Wall -fPIC
# Object Files
OBJ = mod_curse.o
# Header Files
INCLUDE = include/mod_curse.h
# Main Module
mod_setup.so: $(OBJ) $(INCLUDE)
$(CC) -shared -Wl,-soname,mod_curse.so,--no-undefined -o ../../mod_curse.so -lncurses $(OBJ)
# Source Files
mod_curse.o: src/mod_curse.c $(INCLUDE)
$(CC) $(CFLAGS) -c src/mod_curse.c
clean:
rm $(OBJ)
Here are the errors:
3 warnings generated.
clang -shared -Wl,-soname,mod_curse.so,--no-undefined -o ../../mod_curse.so -lncurses mod_curse.o
mod_curse.o: In function `OnLoad':
src/mod_curse.c:(.text+0x81): undefined reference to `initscr'
mod_curse.o: In function `OnDeload':
src/mod_curse.c:(.text+0xb1): undefined reference to `endwin'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mod_setup.so] Error 1
I needed to change my make command to have -lncurses appear after $(OBJ).