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

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...

#include <stdio.h>
#include <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.

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.

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:

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.

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