?
Solved

Compile C function with no Main Block

Posted on 2003-03-20
9
Medium Priority
?
235 Views
Last Modified: 2010-04-15
I am still learning C, and I wanted to know how exactly do I compile a C program containing a function, but that program doesn't have a Main block? I guess what Im trying to say is, make a function in a separate C file that another C program will somehow call? Kind of like a Java class with its main function calling another java class which only contains functions, no main block...

Here is the one C file that I basically am trying to split up into 2 c files, one with the main block and one containing that void hash_pw function...

/*-------------------------------------------------*/

void   hash_pw(char * key, int size, char * pw);

main() {

char   keyval[] = "POID388631";                      /*  Input buffer. */
char   pwval[11];                       /*  Query String env variable   */

printf("\nit works\n");

strcpy(pwval, keyval);

hash_pw(keyval, strlen(keyval), pwval);

printf("String = %s, Hashed = %s\n", keyval, pwval);
}

void hash_pw( char * key, int size, char * pw ) {

      unsigned long n, q;

      n=0;

      for ( q=0; q<strlen(key); q++ ) {
              n = n*1103515245+ (long)key[q];
      }

      for ( q=0; q<size; q++ ) {
              if (n%36 > 9 ) {
                      *(pw+q) = (char) (n%36+87);
              }
              else {
                      *(pw+q) = (char) (n%36+48);
              }

              n = n*1103515245+12345;
              if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;

      }

      *(pw+size) = (char)0;
      return;
}

0
Comment
Question by:oslicko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 6

Expert Comment

by:gj62
ID: 8174965
Just put it in a different file, create a .h file (say hash.h) that  includes the function header, e.g.

void   hash_pw(char * key, int size, char * pw);

and include the .h reference in file that has your main(), e.g.

#include <hash.h>

You can then compile it, and include that file in your project (or reference the obj file in your link instructions if not using a development environment like Visual Studio)
0
 

Expert Comment

by:posternb
ID: 8174968
Depending on your compiler, compile with the "compile only" flag.  For gcc you'd do:

gcc -c foo.c

and you'd get foo.o containing that code.
You can then link objects together to get an executable like:

gcc foo1.o foo2.o -o foo

to get executable 'foo' (one of the objects need a main function)

0
 
LVL 6

Accepted Solution

by:
gj62 earned 140 total points
ID: 8175054
So here's what your files would look like:

-----------------------MAIN.C-------------------------
#include "hash.h"

main() {

char   keyval[] = "POID388631";                      /*  Input buffer. */
char   pwval[11];                       /*  Query String env variable   */

printf("\nit works\n");

strcpy(pwval, keyval);

hash_pw(keyval, strlen(keyval), pwval);

printf("String = %s, Hashed = %s\n", keyval, pwval);
}
----------------------END MAIN.C--------------------------

----------------------HASH.C----------------------------
void hash_pw( char * key, int size, char * pw ) {

     unsigned long n, q;

     n=0;

     for ( q=0; q<strlen(key); q++ ) {
             n = n*1103515245+ (long)key[q];
     }

     for ( q=0; q<size; q++ ) {
             if (n%36 > 9 ) {
                     *(pw+q) = (char) (n%36+87);
             }
             else {
                     *(pw+q) = (char) (n%36+48);
             }

             n = n*1103515245+12345;
             if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;

     }

     *(pw+size) = (char)0;
     return;
}
----------------------END HASH.C-------------------------

-----------------------HASH.H----------------------------
#if !defined( HASH_H )
#define HASH_H

void hash_pw( char * key, int size, char * pw )
 
#endif
----------------------END HASH.H-------------------------

The purpose of the define in HASH.H is to allow you to include hash.h in as many source files as needed, and not get errors due to multiple includes.  

You can then use posternb's command-line link options, or simply include the two source (.c) files in your project if using visual studio, etc...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:gj62
ID: 8175059
whoops - you need a semicolon in HASH.H after the function declaration...

void hash_pw( char * key, int size, char * pw );
0
 

Author Comment

by:oslicko
ID: 8175872
I seem to be getting this error:

> gcc main.c
Undefined                       first referenced
 symbol                             in file
hash                                /var/tmp/cc7UrXv5.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status


Here are my files:
-------------HASH.H--------------

#if !defined( HASH_H )
#define HASH_H

void hash( char * key, int size, char * pw );

#endif

-----------END-HASH.H-------------

--------------HASH.C--------------

void hash( char * key, int size, char * pw ) {
 
      unsigned long n, q;
 
      n=0;
 
      for ( q=0; q<strlen(key); q++ ) {
              n = n*1103515245+ (long)key[q];
      }
 
      for ( q=0; q<size; q++ ) {
              if (n%36 > 9 ) {
                      *(pw+q) = (char) (n%36+87);
              }
              else {
                      *(pw+q) = (char) (n%36+48);
              }
 
              n = n*1103515245+12345;
              if ((*(pw+q)=='0') || (*(pw+q)=='1') || (*(pw+q)=='l') || (*(pw+q)=='o')) q--;
 
      }
 
      *(pw+size) = (char)0;
      return;
}

-----------END-HASH.C-------------

--------------MAIN.C--------------

#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "hash.h"

main() {

char   keyval[] = "POID388631";                    
char   pwval[11];                      
printf("\nit works\n");

strcpy(pwval, keyval);

hash(keyval, strlen(keyval), pwval);

printf("String = %s, Hashed = %s\n", keyval, pwval);

}

-----------END-MAIN.C-------------

Any Ideas?
0
 
LVL 6

Expert Comment

by:gj62
ID: 8175995
Did you compile hash.c and include it in your link?
0
 

Author Comment

by:oslicko
ID: 8176058
I actually forgot to compile hash.c . But now Ive tried again and got the following results:

> gcc -c main.c
> gcc -c hash.c
> gcc hash.o -o main
Undefined                       first referenced
 symbol                             in file
main                                /usr/local/gnu/lib/gcc-lib/sparc-sun-solaris2.7/2.95.2/crt1.o
ld: fatal: Symbol referencing errors. No output written to main
collect2: ld returned 1 exit status

I don't think its missing anything , here are the directory contents now:

remus> ls *.c *.o
hash.c      hash.o     main.c      main.o


0
 
LVL 6

Expert Comment

by:gj62
ID: 8176342
Try

gcc hash.o main.o -o main

0
 

Author Comment

by:oslicko
ID: 8176516
Thanks a lot, you've been really helpful!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question