简体   繁体   中英

Linker Confusion for Declared Functions in Header Files (C)

So I'm in a C programming class and I think of myself as an ok programmer, but I've come across something that I don't quite understand how to think through. I'm writing a compressor that uses several header files and has two c files used for creating the executable. I've included the header files correctly (I think) by having them be in the same directory and saying

#include "myLib.h"

Now. Here's the part I'm stuck on. In one file I have a main method calling the functions declared in the header file. The source code for these functions is in the other .c file I mentioned earlier. When I compile with:

gcc -Wall TestCmp.c LZWCmp.o  

Where TestCmp.c is the file containing the main and LZWCmp.o is the object file for the other .c file. I get compile errors telling me that three of the the four declared methods are undefined references. Why would the linker accept that one of these methods exists but not the other three???

Any help would be appreciated. Thanks!

Here's the source code for TestCmp.c

#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include "LZWCmp.h"
#include "SmartAlloc.h"
#include "MyLib.h"

/*function pointer to the CodeSink function in TestCmp, which function simply prints  each 4-byte uint sent to it as 8 hex digits. It does so 8 integers per line, with one space between each pair of integers, and no space after the final integer, just an EOL.*/
void sink(void *pointer, uint code) {
    printf("%08X ", code);
}

void main() {
   int numCodes; /*Number of codes that compressor starts with understanding*/
   LZWCmp *cmp = malloc(sizeof(struct LZWCmp)); /*allocate memory for compressor state*/
   CodeSink ptr = sink; /*set sodesink pointer to sink function*/
   uchar letter; /*letter for intake and compression*/

   printf("Enter symbol count: ");
   scanf(" %d", &numCodes);
   while(letter != '\n') {
      letter = getchar();
   }

   LZWCmpInit(cmp, numCodes, ptr, NULL); /*Initialize compressor */

   while(letter < UCHAR_MAX) {
      letter = getchar(); 
      LZWCmpEncode(cmp, letter);     /*Send letter to encoder*/ /*FIRST FUNCTION TO NOT WORK*/
   }

   LZWCmpStop(cmp); /*Finish program when finding EOF character*/
   LZWCmpDestruct(cmp); /*Free memory space*/

}

And the source code for myLib.h

#ifndef MYLIB_H
#define MYLIB_H

#define BITS_PER_BYTE 8

typedef unsigned char uchar;
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned short ushort;

#ifdef LITTLE_ENDIAN

#define UShortEndianXfer(val) ((val) >> 8 | (val) << 8}

#else

#define UShortEndianXfer(val) (val)

#endif

#endif

and the source for LZWCmp.h

#ifndef LZW_H
#define LZW_H

#include "MyLib.h"

#define RECYCLE_CODE 4096  // Recycle dictionary rather than add this code

/* Function pointer to method to call when a code is completed and ready for
 * transmission or whatever.  The void * parameter can point to anything,
 * and gives hidden information to the function so that it can know what
 * file, socket, etc. the code is going to.  The uint is the next 32 bits
 * worth of compressed output. */
typedef void (*CodeSink)(void *, uint code);

/* One node in a trie representing the current dictionary.  Use symbols
 * to traverse the trie until reaching a point where the link for a
 * symbol is null.  Use the code for the prior link, and add a new code in
 * this case.  Each node has as many links and codes as there are symbols */
typedef struct TrieNode {
    ushort *codes;
    struct TrieNode **links;
} TrieNode;

/* Current state of the LZW compressor. */
typedef struct LZWCmp {
   TrieNode *head;   /* Head pointer to first TrieNode */
   CodeSink sink;   /* Code sink to send bits to */
   void *sinkState;  /* Unknown object to send to sink for state */
   int numSyms;      /* Symbol count, also size of TrieNodes */
   int nextCode;     /* Next code to be assigned */
   int numBits;      /* Number of bits per code currently */
   uint nextInt;     /* Partially-assembled next int of output */
   int bitsUsed;     /* Number of valid bits in top portion of nextInt */
   TrieNode *curLoc; /* Current position in trie */
   short lastSym;    /* Most recent symbol encoded */
} LZWCmp;

/* Initialize a LZWCmp given the number of symbols and the CodeSink
 * to which to send completed codes; */
void LZWCmpInit(LZWCmp *cmp, int numSyms, CodeSink sink, void *sinkState);

/* Encode "sym" using LZWCmp. Zero or more calls of the code sink
 * may result */
 void LZWCmpEncode(LZWCmp *cmp, uchar sym);

/* Mark end of encoding (send next code value to code sink) */
void LZWCmpStop(LZWCmp *cmp);

/* Free all storage associated with LZWCmp (not the sinkState, though,
 * which is "owned" by the caller */
void LZWCmpDestruct(LZWCmp *cmp);

#endif

As for the object I'm compiling with, it is an .o file given to us by the professor containing the four functions given in LZWCmp.h file. If our TestCmp.c file works correctly, than I should be able to access the functions inside of the LZCmp.o file without a problem.

OK.. with few modification to your code, I was able to (link and) compile your code.. The files are as follows:

Filename: TestCmp.c

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <limits.h>
#include "LZWCmp.h"
//#include "SmartAlloc.h"
#include "MyLib.h"

/*function pointer to the CodeSink function in TestCmp, which function simply prints  each 4-byte uint sent to it as 8 hex digits. It does so 8 integers per line, with one space between each pair of integers, and no space after the final integer, just an EOL.*/
void sink(void *pointer, uint code) {
    printf("%08X ", code);
}

int main(void) 
{
   int numCodes; /*Number of codes that compressor starts with understanding*/
   LZWCmp *cmp = malloc(sizeof(struct LZWCmp)); /*allocate memory for compressor state*/
   CodeSink ptr = sink; /*set sodesink pointer to sink function*/
   uchar letter; /*letter for intake and compression*/

   printf("Enter symbol count: ");
   scanf(" %d", &numCodes);
   while(letter != '\n') {
      letter = getchar();
   }

   LZWCmpInit(cmp, numCodes, ptr, NULL); /*Initialize compressor */

   while(letter < UCHAR_MAX) {
      letter = getchar(); 
      LZWCmpEncode(cmp, letter);     /*Send letter to encoder*/ /*FIRST FUNCTION TO NOT WORK*/
   }

   LZWCmpStop(cmp); /*Finish program when finding EOF character*/
   LZWCmpDestruct(cmp); /*Free memory space*/

    return 0;

}

Filename: MyLib.h

#ifndef MYLIB_H
#define MYLIB_H

#define BITS_PER_BYTE 8

typedef unsigned char uchar;
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned short ushort;

#ifdef LITTLE_ENDIAN

#define UShortEndianXfer(val) ((val) >> 8 | (val) << 8}

#else

#define UShortEndianXfer(val) (val)

#endif

#endif

Filename: LZWCmp.h

#ifndef LZW_H
#define LZW_H
#include <stdio.h>
#include "MyLib.h"

#define RECYCLE_CODE 4096  // Recycle dictionary rather than add this code

/* Function pointer to method to call when a code is completed and ready for
 * transmission or whatever.  The void * parameter can point to anything,
 * and gives hidden information to the function so that it can know what
 * file, socket, etc. the code is going to.  The uint is the next 32 bits
 * worth of compressed output. */
typedef void (*CodeSink)(void *, uint code);

/* One node in a trie representing the current dictionary.  Use symbols
 * to traverse the trie until reaching a point where the link for a
 * symbol is null.  Use the code for the prior link, and add a new code in
 * this case.  Each node has as many links and codes as there are symbols */
typedef struct TrieNode {
    ushort *codes;
    struct TrieNode **links;
} TrieNode;

/* Current state of the LZW compressor. */
typedef struct LZWCmp {
   TrieNode *head;   /* Head pointer to first TrieNode */
   CodeSink sink;   /* Code sink to send bits to */
   void *sinkState;  /* Unknown object to send to sink for state */
   int numSyms;      /* Symbol count, also size of TrieNodes */
   int nextCode;     /* Next code to be assigned */
   int numBits;      /* Number of bits per code currently */
   uint nextInt;     /* Partially-assembled next int of output */
   int bitsUsed;     /* Number of valid bits in top portion of nextInt */
   TrieNode *curLoc; /* Current position in trie */
   short lastSym;    /* Most recent symbol encoded */
} LZWCmp;

/* Initialize a LZWCmp given the number of symbols and the CodeSink
 * to which to send completed codes; */
void LZWCmpInit(LZWCmp *cmp, int numSyms, CodeSink sink, void *sinkState);

/* Encode "sym" using LZWCmp. Zero or more calls of the code sink
 * may result */
 void LZWCmpEncode(LZWCmp *cmp, uchar sym);

/* Mark end of encoding (send next code value to code sink) */
void LZWCmpStop(LZWCmp *cmp);

/* Free all storage associated with LZWCmp (not the sinkState, though,
 * which is "owned" by the caller */
void LZWCmpDestruct(LZWCmp *cmp);

#endif

Filename: LZWCmp.c (this is what I've introduced which will be passed to the make command - see below)

#include "LZWCmp.h"

void LZWCmpInit(LZWCmp *cmp, int numSyms, CodeSink sink, void *sinkState)
{
    printf("LZWCmpInit \n");
}

void LZWCmpEncode(LZWCmp *cmp, uchar sym)
{
    printf("LZWCmpEncode \n");   
}

void LZWCmpStop(LZWCmp *cmp)
{
    printf("LZWCmpStop \n");
}

void LZWCmpDestruct(LZWCmp *cmp)
{
    printf("LZWCmpDestruct \n");    
}

Make command:

gcc -Wall TestCmp.c LZWCmp.h MyLib.h LZWCmp.c

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM