LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-04-2012, 11:19 AM   #1
oced
LQ Newbie
 
Registered: May 2012
Posts: 2

Rep: Reputation: Disabled
"undefined reference to" errors when i compile LzmaUtil.c


This code is from http://www.7-zip.org
Code PATH is lzma920/C/Util/Lzma/LzmaUtil.c

Code:
/* LzmaUtil.c -- Test application for LZMA compression

2010-09-20 : Igor Pavlov : Public domain */



#define _CRT_SECURE_NO_WARNINGS



#include <stdio.h>

#include <stdlib.h>

#include <string.h>



#include "../../Alloc.h"

#include "../../7zFile.h"

#include "../../7zVersion.h"

#include "../../LzmaDec.h"

#include "../../LzmaEnc.h"



const char *kCantReadMessage = "Can not read input file";

const char *kCantWriteMessage = "Can not write output file";

const char *kCantAllocateMessage = "Can not allocate memory";

const char *kDataErrorMessage = "Data error";



static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }

static void SzFree(void *p, void *address) { p = p; MyFree(address); }

static ISzAlloc g_Alloc = { SzAlloc, SzFree };



void PrintHelp(char *buffer)

{

  strcat(buffer, "\nLZMA Utility " MY_VERSION_COPYRIGHT_DATE "\n"

      "\nUsage:  lzma <e|d> inputFile outputFile\n"

             "  e: encode file\n"

             "  d: decode file\n");

}



int PrintError(char *buffer, const char *message)

{

  strcat(buffer, "\nError: ");

  strcat(buffer, message);

  strcat(buffer, "\n");

  return 1;

}



int PrintErrorNumber(char *buffer, SRes val)

{

  sprintf(buffer + strlen(buffer), "\nError code: %x\n", (unsigned)val);

  return 1;

}



int PrintUserError(char *buffer)

{

  return PrintError(buffer, "Incorrect command");

}



#define IN_BUF_SIZE (1 << 16)

#define OUT_BUF_SIZE (1 << 16)



static SRes Decode2(CLzmaDec *state, ISeqOutStream *outStream, ISeqInStream *inStream,

    UInt64 unpackSize)

{

  int thereIsSize = (unpackSize != (UInt64)(Int64)-1);

  Byte inBuf[IN_BUF_SIZE];

  Byte outBuf[OUT_BUF_SIZE];

  size_t inPos = 0, inSize = 0, outPos = 0;

  LzmaDec_Init(state);

  for (;;)

  {

    if (inPos == inSize)

    {

      inSize = IN_BUF_SIZE;

      RINOK(inStream->Read(inStream, inBuf, &inSize));

      inPos = 0;

    }

    {

      SRes res;

      SizeT inProcessed = inSize - inPos;

      SizeT outProcessed = OUT_BUF_SIZE - outPos;

      ELzmaFinishMode finishMode = LZMA_FINISH_ANY;

      ELzmaStatus status;

      if (thereIsSize && outProcessed > unpackSize)

      {

        outProcessed = (SizeT)unpackSize;

        finishMode = LZMA_FINISH_END;

      }

      

      res = LzmaDec_DecodeToBuf(state, outBuf + outPos, &outProcessed,

        inBuf + inPos, &inProcessed, finishMode, &status);

      inPos += inProcessed;

      outPos += outProcessed;

      unpackSize -= outProcessed;

      

      if (outStream)

        if (outStream->Write(outStream, outBuf, outPos) != outPos)

          return SZ_ERROR_WRITE;

        

      outPos = 0;

      

      if (res != SZ_OK || thereIsSize && unpackSize == 0)

        return res;

      

      if (inProcessed == 0 && outProcessed == 0)

      {

        if (thereIsSize || status != LZMA_STATUS_FINISHED_WITH_MARK)

          return SZ_ERROR_DATA;

        return res;

      }

    }

  }

}



static SRes Decode(ISeqOutStream *outStream, ISeqInStream *inStream)

{

  UInt64 unpackSize;

  int i;

  SRes res = 0;



  CLzmaDec state;



  /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */

  unsigned char header[LZMA_PROPS_SIZE + 8];



  /* Read and parse header */



  RINOK(SeqInStream_Read(inStream, header, sizeof(header)));



  unpackSize = 0;

  for (i = 0; i < 8; i++)

    unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8);



  LzmaDec_Construct(&state);

  RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc));

  res = Decode2(&state, outStream, inStream, unpackSize);

  LzmaDec_Free(&state, &g_Alloc);

  return res;

}



static SRes Encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize, char *rs)

{

  CLzmaEncHandle enc;

  SRes res;

  CLzmaEncProps props;



  rs = rs;



  enc = LzmaEnc_Create(&g_Alloc);

  if (enc == 0)

    return SZ_ERROR_MEM;



  LzmaEncProps_Init(&props);

  res = LzmaEnc_SetProps(enc, &props);



  if (res == SZ_OK)

  {

    Byte header[LZMA_PROPS_SIZE + 8];

    size_t headerSize = LZMA_PROPS_SIZE;

    int i;



    res = LzmaEnc_WriteProperties(enc, header, &headerSize);

    for (i = 0; i < 8; i++)

      header[headerSize++] = (Byte)(fileSize >> (8 * i));

    if (outStream->Write(outStream, header, headerSize) != headerSize)

      res = SZ_ERROR_WRITE;

    else

    {

      if (res == SZ_OK)

        res = LzmaEnc_Encode(enc, outStream, inStream, NULL, &g_Alloc, &g_Alloc);

    }

  }

  LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);

  return res;

}



int main2(int numArgs, const char *args[], char *rs)

{

  CFileSeqInStream inStream;

  CFileOutStream outStream;

  char c;

  int res;

  int encodeMode;

  Bool useOutFile = False;



  FileSeqInStream_CreateVTable(&inStream);

  File_Construct(&inStream.file);



  FileOutStream_CreateVTable(&outStream);

  File_Construct(&outStream.file);



  if (numArgs == 1)

  {

    PrintHelp(rs);

    return 0;

  }



  if (numArgs < 3 || numArgs > 4 || strlen(args[1]) != 1)

    return PrintUserError(rs);



  c = args[1][0];

  encodeMode = (c == 'e' || c == 'E');

  if (!encodeMode && c != 'd' && c != 'D')

    return PrintUserError(rs);



  {

    size_t t4 = sizeof(UInt32);

    size_t t8 = sizeof(UInt64);

    if (t4 != 4 || t8 != 8)

      return PrintError(rs, "Incorrect UInt32 or UInt64");

  }



  if (InFile_Open(&inStream.file, args[2]) != 0)

    return PrintError(rs, "Can not open input file");



  if (numArgs > 3)

  {

    useOutFile = True;

    if (OutFile_Open(&outStream.file, args[3]) != 0)

      return PrintError(rs, "Can not open output file");

  }

  else if (encodeMode)

    PrintUserError(rs);



  if (encodeMode)

  {

    UInt64 fileSize;

    File_GetLength(&inStream.file, &fileSize);

    res = Encode(&outStream.s, &inStream.s, fileSize, rs);

  }

  else

  {

    res = Decode(&outStream.s, useOutFile ? &inStream.s : NULL);

  }



  if (useOutFile)

    File_Close(&outStream.file);

  File_Close(&inStream.file);



  if (res != SZ_OK)

  {

    if (res == SZ_ERROR_MEM)

      return PrintError(rs, kCantAllocateMessage);

    else if (res == SZ_ERROR_DATA)

      return PrintError(rs, kDataErrorMessage);

    else if (res == SZ_ERROR_WRITE)

      return PrintError(rs, kCantWriteMessage);

    else if (res == SZ_ERROR_READ)

      return PrintError(rs, kCantReadMessage);

    return PrintErrorNumber(rs, res);

  }

  return 0;

}



int MY_CDECL main(int numArgs, const char *args[])

{

  char rs[800] = { 0 };

  int res = main2(numArgs, args, rs);

  fputs(rs, stdout);

  return res;

}
When i compile this code in terminal:
rosyid@ubuntu:~/LZMA/lzma920/C/Util/Lzma$ gcc LzmaUtil.c -o LzmaUtil.o

i get "undefined reference to" errors like this:

/tmp/ccG7tMQ3.o: In function `SzAlloc':
LzmaUtil.c:(.text+0xd): undefined reference to `MyAlloc'
/tmp/ccG7tMQ3.o: In function `SzFree':
LzmaUtil.c:(.text+0x20): undefined reference to `MyFree'
/tmp/ccG7tMQ3.o: In function `Decode2':
LzmaUtil.c:(.text+0x1a7): undefined reference to `LzmaDec_Init'
LzmaUtil.c:(.text+0x2d7): undefined reference to `LzmaDec_DecodeToBuf'
/tmp/ccG7tMQ3.o: In function `Decode':
LzmaUtil.c:(.text+0x41e): undefined reference to `SeqInStream_Read'
LzmaUtil.c:(.text+0x4d4): undefined reference to `LzmaDec_Allocate'
LzmaUtil.c:(.text+0x53d): undefined reference to `LzmaDec_Free'
/tmp/ccG7tMQ3.o: In function `Encode':
LzmaUtil.c:(.text+0x595): undefined reference to `LzmaEnc_Create'
LzmaUtil.c:(.text+0x5b3): undefined reference to `LzmaEncProps_Init'
LzmaUtil.c:(.text+0x5c5): undefined reference to `LzmaEnc_SetProps'
LzmaUtil.c:(.text+0x5f2): undefined reference to `LzmaEnc_WriteProperties'
LzmaUtil.c:(.text+0x695): undefined reference to `LzmaEnc_Encode'
LzmaUtil.c:(.text+0x6b3): undefined reference to `LzmaEnc_Destroy'
/tmp/ccG7tMQ3.o: In function `main2':
LzmaUtil.c:(.text+0x6e8): undefined reference to `FileSeqInStream_CreateVTable'
LzmaUtil.c:(.text+0x6f6): undefined reference to `File_Construct'
LzmaUtil.c:(.text+0x701): undefined reference to `FileOutStream_CreateVTable'
LzmaUtil.c:(.text+0x70f): undefined reference to `File_Construct'
LzmaUtil.c:(.text+0x7f2): undefined reference to `InFile_Open'
LzmaUtil.c:(.text+0x835): undefined reference to `OutFile_Open'
LzmaUtil.c:(.text+0x87d): undefined reference to `File_GetLength'
LzmaUtil.c:(.text+0x8df): undefined reference to `File_Close'
LzmaUtil.c:(.text+0x8ed): undefined reference to `File_Close'
collect2: ld returned 1 exit status

How to solve this problem? Please help me. I beg someone can help me..

Last edited by oced; 05-04-2012 at 11:22 AM. Reason: i forgot to type CODE tag
 
Old 05-04-2012, 12:23 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 651

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally Posted by oced View Post
This code is from http://www.7-zip.org
When i compile this code in terminal:
rosyid@ubuntu:~/LZMA/lzma920/C/Util/Lzma$ gcc LzmaUtil.c -o LzmaUtil.o
Is there a reason you use a .o extension for the compile output? This extension is usually used for object files. If you wanted to only compile the file into an object file, you should add the -c option to gcc.

Quote:
Originally Posted by oced View Post
i get "undefined reference to" errors like this:
...
How to solve this problem? Please help me. I beg someone can help me..
What you get is linker errors. Linker complains that it can not find objects (functions, variables,...) that are referenced in the source file. These are defined in sources two levels above the current directory which you also have to compile and link to the program. Why don't you use the makefile that is inside the current directory?
This should work for you:
Code:
make -f makefile.gcc

Last edited by millgates; 05-04-2012 at 12:25 PM.
 
1 members found this post helpful.
Old 05-04-2012, 01:01 PM   #3
oced
LQ Newbie
 
Registered: May 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
This should work for you:
Code:
make -f makefile.gcc
Thank you very much.. It Works!!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Multiple definition of" and "undefined reference to" in QT Creator with QJson lib echelon89 Programming 1 05-01-2009 06:48 AM
Fixed! "Undefined reference to.." errors talent422 Programming 2 01-16-2006 08:51 AM
Undefined Screen " XFree86" reference by serverLayout "XFree86 Configured" comox *BSD 7 01-17-2005 05:47 PM
ncurses compile error: "undefined reference" Mr. Eek Linux From Scratch 1 05-12-2003 04:25 PM


All times are GMT -5. The time now is 10:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration