Roger S
Posts: 9
Joined: Sun Jan 07, 2018 10:05 pm

Failure to link a library

Sun Nov 18, 2018 10:45 pm

I am trying to link a C program into some assembly code. I am using gcc as and ld as required and they appear to generate the required file. In detail I do the following:-

as -g -o forth.o forth.s
gcc -c -o ccalls.o calls.c
ld -o forth forth.o calls.o -lncurses

However when I try to run it I get:-

pi@raspberrypi:~ $ ./forth
bash: ./forth: No such file or directory
pi@raspberrypi:~ $ ls -l forth
-rwxr-xr-x 1 pi pi 22924 Nov 18 22.16 forth

As can be seen the file is made and has execute permission but will not run.
If I do:-

as -g -o forth.o forth.s
ld -o forth forth.o

Then the program runs ( the program does not actually call the C function yet so no errors are created).
However If I do:-

as -g -o forth.o forth.s
ld -o forth forth.o calls.o

then the file is not found. I wrote a c wrapper to call and test ccalls which worked as expected so I don't think it is the actual code at fault.
I conclude that it is linking the ncurses library into assembler code which is causing the problem but I have no idea why.

Regards Roger

LdB
Posts: 1585
Joined: Wed Dec 07, 2016 2:29 pm

Re: Failure to link a library

Mon Nov 19, 2018 12:49 am

1.) First I would refrain from calling the linker directly it is prone to problems

2.) You have a typo which if you cut and paste and it really says that will be a problem

Code: Select all

gcc -c -o ccalls.o calls.c    /*That compiles calls.c to ccalls.o but in the next line you expect calls.o*/
3.) You are praying the compiler defaults to the CPU, optimization and error reporting levels you want.
Any or all of those could be wrong you really should set them.

So if your defaults are right what you really want is
as -g -o forth.o forth.s
gcc -c -o calls.o calls.c
gcc -o forth forth.o calls.o -lncurses
However I really would look at adding some compiler flags like -wall, -O2 etc :-)

Roger S
Posts: 9
Joined: Sun Jan 07, 2018 10:05 pm

Re: Failure to link a library

Tue Nov 20, 2018 12:10 am

Thank you for the reply.
Using gcc instead of ld seems to have done the trick.
However I now get segmentation faults when I run it. The code I am calling is
/* ccalls.c */
#include <ncurses.h>

char inchar()
{
int ch;
initscr();
cbreak();
ch =getch();
endwin();
return (ch);
}
And it is called by
key_c:
bl inchar @ Call c
stmea DSTK!, {r0} @ Put on data stack
b NEXT

Debugging (gdb) the code I can follow it for hundreds of instructions into the lcurses library code but I loose track of what the code is doing before it fails with the message:- dl-lookup.c: No such file or derectory.
I can call it in c and it works correctly
/* ctest */
#include<stdio.h>
char inchar();

int main()
{
char ch:


Roger S
Posts: 9
Joined: Sun Jan 07, 2018 10:05 pm

Re: Failure to link a library

Tue Nov 20, 2018 12:24 am

Thank you for the reply.
Using gcc instead of ld seems to have done the trick.
However I now get segmentation faults when I run it. The code I am calling is
/* ccalls.c */
#include <ncurses.h>

char inchar()
{
int ch;
initscr();
cbreak();
ch =getch();
endwin();
return (ch);
}
And it is called by
key_c:
bl inchar @ Call c
stmea DSTK!, {r0} @ Put on data stack
b NEXT

Debugging (gdb) the code I can follow it for hundreds of instructions into the lcurses library code but I loose track of what the code is doing before it fails with the message:-

dl-lookup.c: No such file or directory.

I can call it in c and it works correctly
/* ctest */
#include<stdio.h>
char inchar();

int main()
{
char ch:
ch = inchar();
printf(" Char was %c "), ch );

return(0);
It seems that "inchar" is called correctly and starts running but when called from assembler is unable to find the requisite files despite gcc not complaining about missing files.

Regards Roger

LdB
Posts: 1585
Joined: Wed Dec 07, 2016 2:29 pm

Re: Failure to link a library

Tue Nov 20, 2018 1:02 am

You are in the baremetal section and we would provide our own implementation of the console.
You are really in the wrong part of the forum its really a linux question about ncurses.

However what you know is the C <stdio> implementation works, so its the nurse implementation that is the problem.

When you put this at the end (-lncurses) you instructed gcc to link with that library rather than it's standard library it uses and it's that library that isn't working.

So we get down to is everything in linux updated and installed properly with all dependencies etc.

So you need to do all that "sudo apt-get update" junk they do on linux first. If you still have problem ask on the Raspbian Forum (or whatever flavour linux you are using) about ncurses installation issues.

Roger S
Posts: 9
Joined: Sun Jan 07, 2018 10:05 pm

Re: Failure to link a library

Wed Nov 21, 2018 12:02 am

I realise that I am in the Bare metal, Assembly language section. Elsewhere all I get are replies telling me how to call assembler from c or suggesting that assembler is too hard/not suitable/otherwise no good but no constructive help.

Can you tell me what sort of console you use. I need USB capability and so far I have not found a driver that I can get to work which is why I am having to use Linux.

Regards Roger

LdB
Posts: 1585
Joined: Wed Dec 07, 2016 2:29 pm

Re: Failure to link a library

Wed Nov 21, 2018 1:49 am

Generally that is a larger question with what you are going to do on the Pi.

In baremetal we have nothing, no file system, no screen no peripherals of any kind. From that we write everything ourself and we build the system up. It's a place you go to either learn things or develop things.

So in baremetal for the USB there are a number of implementations out
I have one
https://github.com/LdB-ECM/Raspberry-Pi ... m32_64_USB
rst has one
https://github.com/rsta2/uspi
There is another called teensy and there are probably more by now.

You can also optionally just add a serial to usb adapter so the usb keyboard comes in on the serial port.
So you can ignore all the USB stuff totally.
https://www.sparkfun.com/products/9873

From there you step up to task switchers which is much the same as any microcontroller.
There are many examples out there, I just put a port of FreeRTOS on my own site.
There is also ChiBios (https://github.com/steve-bate/ChibiOS-RPi)
There are also commercial stuff available at that level because the ARM processor on the Pi is
more generally supported. That includes ARM's own stuff but it does cost $$$ but you get support.

Up from there you have teaching O/S systems things like Circle (on rst site), Xinu (I have a port on my site),
XV6, Plan 9 and a couple of others.

Finally at the very top you have the approved and supported Linux Distros where you are playing.

So where you would start depends on what you are doing and is it a commercial use or just hobby?

Return to “Bare metal, Assembly language”