Visit Our UNIX and Linux User Community


compile funtion


 
Thread Tools Search this Thread
Top Forums Programming compile funtion
# 1  
Old 01-25-2002
Question compile funtion

Hi,

This is the first time, i am doing UNIX & C stuff.

I have written one funtion like

#include <stdio.h>
seq(num)
int num;
{
int i,sum;
for (i=1; i<=num;i++)
sum+=i;
return(sum);
}

this seq funtion should have return values to unix script.
like

..
a=$(seq 10)
print $a
..

problem is, when i tried to compile above funtion, system gives

/usr/ccs/bin/ld: Unsatisfied symbols:
main

Is there any way to compile without main function. I am using HP-UX 11.0 and compiler is cc.

Thanks in advance
# 2  
Old 01-26-2002
The shell runs c programs, not c functions. For example in the korn shell you could:
x=$(date)
But date is a complete program. It decodes any arguments passed to it. And it writes its output to stdout. You need to write a program that does all of these things as well. On HP-UX, cc is an old compiler. But here is code that will work with it that does what you want...
Code:
#include &lt;stdio.h>
#include &lt;stdio.h>
void main(argc, argc)
char *argv[];
{
     int num,i,sum;
     num=atoi(argv[1])
     for(i=1; i<=num; i++)
          sum+=i;
     printf("%d\n", sum);
     exit(0);
}

Sometimes, with larger projects, you will want to compile a function separately. You can use the -c flag to do this. But that's not what you want here.

And finally, with ksh-92, it is possible to write a c function and dynamically load it into the ksh executable and then invoke it from ksh. But you are probably using ksh-88 which comes standard with HP-UX. HP-UX does have /usr/dt/bin/dtksh which is a heavily extended version of ksh-92. But extending it further with user-written functions might be a bit much for you until you get some more c experience.
# 3  
Old 01-28-2002
Network

You wrote :
Sometimes, with larger projects, you will want to compile a function separately. You can use the -c flag to do this.
----

Basically i do some plug-in in ksh. what you said is correct, i am using ksh-88 only.

In my project, I need to write some tools(functions) in C and compile a function separately, and then call that funtion's executables in ksh script.

I have included -c flag in cc. but it is generating object files only

like

$ cc -c seq.c
$
$
$ cat seq.c

#include <stdio.h>
seq(num)
int num;
{
int i,sum;
for (i=1; i<=num;i++)
sum+=i;
return(sum);
}
$ ll seq*
-rw-rw-r-- 1 ict ict 99 Jan 23 17:16 seq.c
-rw-rw-r-- 1 ict ict 576 Jan 28 09:47 seq.o

Can you please help out in this problem. how can i make executables using this. I hope, this might be very simple thing for you.

Thanks lot.
# 4  
Old 01-28-2002
You don't make executables out of functions. You must have a main to make an executable. main() itself is a function, but it's a special function. An executable is built with a assembly language module often called crt0.o that gets the ball rolling, but it must have a main() to call. An executable will always have a main(). If you want to call an executable from ksh, you must proceed as I showed above. If you want to add one of your functions into the ksh executable, that is different.

When I said that ksh-92 could make use of a function that was dymanically loaded into it, I didn't mean to imply that ksh-88 could as well. Let me be more plain: This cannot be done with ksh-88.

But with ksh-92, it is possible, although I do not know exactly how to do this. The New KornShell Command and Programming Language by Morris I Bolsky and David G Korn states:
Quote:
In addition, on systems that have the ability to link in code at runtime, the builtin command can be used to add code libraries and built-in commands. The description of how to create these commands is outside the scope of this book.
At other places in the book I can see that you use the -f option to builtin to specify a path of a shared library which must contain a function named b_name for each builtin named name that you want to add. But I have no idea how ksh will pass arguments to that function. Since variables can be integer or strings there must be some information on the structure of the variable somewhere.

I suppose that I would start by downloading the source code to ksh and studying it to determine exactly how it loads and invokes functions. I'm going to guess that you're not clear on how to create a shared library. This is described on the ld man page, but I would also suggest that you obtain a copy of HP-UX Linker and Libraries User's Guide and read it as well.

I don't want to be snotty here, but my sense is that you're biting off more than you can chew. But if I'm wrong and you get this to work, please post again and explain how you did it. I'd like to know how to do this myself.
# 5  
Old 02-01-2002
It is not very clear what you are trying to do. Is that you want to create a separate library with functions and then have an executable calling them, or the difficult endeavour to encapsulate the code as "internal" ksh commands?.

If it is the first, you just have to use "gcc -c" to create an object file from your functions source file test.c (i.e. test.o). Then use gcc -shared -o libname.so test.o to get your libname.so shared library file. Put it in the dir /lib. Then whenever you compile any new source .c file you make you have to compile with the -lname directive. Before you have to issue the ldopen() command. This is very simplified and you have to consult books how to do it exactly, it was just an example to show you the flow.

If it is the 2nd case, it much more difficult than this as Perderabo stated, but it includes recompiling sdt OS shared libraries with specific format (I have never done it). If still this is your interest, I suggest you play around with tcsh, a shell you can find the code on the net and will not interfere with standard OS shells.

Hope

Previous Thread | Next Thread
Test Your Knowledge in Computers #18
Difficulty: Easy
Facebook purchased the popular video teleconferencing software 'Skype' from Microsoft.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find -exec How to add additional parameter when calling a funtion

Hello Current working script is : # # my_script BEGIN # function a_function { FIRST_PARAM="$1" DO_SOMETHING "$FIRST_PARAM" } export -f a_function START_HERE="/home/some_user/Documents" find $START_HERE" -exec bash -c 'a_function "$0" ' {} \; (5 Replies)
Discussion started by: jcdole
5 Replies

2. Shell Programming and Scripting

multiple looping with case and funtion showing error, Please help

Hello All, I have code as follows :- while true do {opening a case1 statement} 1) {opening another case2 statement} {closing case 2} 2) Showing error for "2)" as Syntax error at line 59 : `)' is not expected. *) {closing case 1} ... (5 Replies)
Discussion started by: Renjesh
5 Replies

3. Shell Programming and Scripting

how to check/display/edit funtion

Hi, I have this output: $ type msq msq is a function I thought it an alias, can you tell me how to I can find this function and where ? Thanks for help. I'm on RH and HP-UX Best T Continued here (0 Replies)
Discussion started by: trento17
0 Replies

4. UNIX for Dummies Questions & Answers

Compiling gcc to compile make to compile yaboot

I have just installed OpenBSD on a 333MHz PPC iMac G3. It has a 6GB HDD that has been partitioned as 1GB MacOS 8.5.1, 3GB MacOS X 10.3.9, 2GB OpenBSD 4.8. I now need to install a bootloader so that my computer can recognize the OpenBSD partition at startup. I have been trying to install... (0 Replies)
Discussion started by: t04st3r
0 Replies

5. Shell Programming and Scripting

Shell Script not working using for loop and a funtion

Hi- Here is the shell script that for some reason is not returning results: #! /bin/ksh - avg() { AVG=0 typeset SUM=0 if then echo "You must have at least two numbers" else for NUM in "$*" do ... (2 Replies)
Discussion started by: koomba
2 Replies

6. Programming

help to compile

Hello everybody, I have a small opensource project http://hpaftpd.sourceforge.net (single-threaded ftp-server). It tested with FreeBSD and Linux. Can anybody try it with another UNIX system ? I'm interesting about HP/UX and Solaris. I would very much appreciate receiving any results about it. ... (2 Replies)
Discussion started by: wwwdev
2 Replies

7. Cybersecurity

Hash Funtion properties

Here are some desirable properties for cryptographic hash functions: These properties below are generally considered prerequisites: * Preimage resistant: given h it should be hard to find any m such that h = hash(m). * Second preimage resistant: given an input m1, it should be hard... (1 Reply)
Discussion started by: newkidintown
1 Replies

8. SCO

Unable to use funtion keys with Kea term

Hi I have a server SCO_SV mantrak 3.2 5.0.5 i386 and i use a terminal emulator called Kea 420 term ! when i log in to the session i am unable to use the funtions keys then i reset the server and works fine just some minutes any comment ? i really apreciate your help regards... (1 Reply)
Discussion started by: Rdavila
1 Replies

9. Post Here to Contact Site Administrators and Moderators

Delete funtion

Neo, eariler today i had tossed up a post then realized i didnt want it up so i tried to delete it and it wouldnt let me. is this a functionality that you have active? To allow a user to delete any topic they started would be a great thing, i think it will also help on not haveing double posts... (2 Replies)
Discussion started by: Optimus_P
2 Replies

10. UNIX for Dummies Questions & Answers

i see "{}" in various commands around here what is there funtion?

IE: find / -type f -exec grep email@host {} /dev/null \; or find /home/domains/*/ -type d -exec chmod 777 {} \; is the {} \; part of the -type funciton? this is what i understand about the following commands. 1) start the find at the root dir. a) only looking for flatfiles b)... (4 Replies)
Discussion started by: Optimus_P
4 Replies

Featured Tech Videos