0

I am using JaetBrains' Clion with MinGW 3.2.1 on windows. and I'm trying to build a project in c. I keep getting the following linkage error: undefined reference to `printf'

any Idea How to solve it?

this is my code:

#include <fcntl.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h> // for time measurement
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <libintl.h>

#define BUFFERSIZE 1

int main(int argc, char** argv) {
   assert(argc == 3);

char* inputDirPath = argv[0];
char* keyFilePath = argv[1];
char* outputDirPath = argv[2];

// open key file
int key_fd = open(keyFilePath, O_RDONLY);

if (key_fd < 0) {
    printf("Failed opening Key file %s. Error: %s\n", keyFilePath, strerror(errno));
    return errno;
}

// making sure the file is not empty
char keyFirstChar;
if (read(key_fd, (void*)keyFirstChar, 1) == 0)
{
    printf("Error. Key file is empty %s.", keyFilePath);
    return errno;
}
else {
    // go back to the begining of the file.
    assert(!close(key_fd));
    key_fd = open(keyFilePath, O_RDONLY);

    if (key_fd < 0) {
        printf("Failed opening Key file %s. Error: %s\n", keyFilePath,

               strerror(errno)

        );
        return errno;
    }
}


// Temp file name
char inputFilepath[200] ;

struct dirent *dirEntity;
DIR *inputDir_dfd;

// open directory stream
assert((inputDir_dfd = opendir(inputDirPath)) != NULL);


while ((dirEntity = readdir(inputDir_dfd)) != NULL)
{
    // full path to input file
    sprintf(inputFilepath, "%s/%s",inputDirPath, dirEntity->d_name) ;

    // call stat to get file metadata
    struct stat statbuf ;
    assert( stat(inputFilepath,&statbuf ) != -1 );

    // skip directories
    if ( ( statbuf.st_mode & S_IFMT ) == S_IFDIR )
    {
        continue;
    }

    // open input file
    int inputFile_fd = open(inputFilepath, O_RDONLY);

    if (inputFile_fd < 0) {
        printf("Failed opening file in input directory, %s. Error: %s\n", inputFilepath, strerror(errno));
        return errno;
    }

    // Temp file name
    char outputFilePath[200] ;

    // full path to file
    sprintf(outputFilePath, "%s/%s",outputDirPath, dirEntity->d_name) ;

    // open input file
    int outputFile_fd = open(outputFilePath, O_WRONLY | O_CREAT | O_TRUNC);

    if (outputFile_fd < 0) {
        printf("Failed opening file in output directory, %s. Error: %s\n", outputFilePath, strerror(errno));
        return errno;
    }

    char inputFileBuf[BUFFERSIZE];
    while (read(inputFile_fd, inputFileBuf, BUFFERSIZE) == BUFFERSIZE){

            char keyFileBuf[BUFFERSIZE];

           if (read(key_fd, keyFileBuf, BUFFERSIZE) == 0) {
               assert(!close(key_fd));
               key_fd = open(keyFilePath, O_RDONLY);

               if (key_fd < 0) {
                   printf("Failed opening Key file %s. Error: %s\n", keyFilePath, strerror(errno));
                   return errno;
               }
               read(key_fd,keyFileBuf, BUFFERSIZE);
           }

           char outputToWrite[BUFFERSIZE];
           int i;
           for(i = 0; i < BUFFERSIZE; i++){
               outputToWrite[i] = keyFileBuf[i] ^ inputFileBuf[1];
           }

           if( write(outputFile_fd, outputToWrite, BUFFERSIZE) == -1){
               printf("Failed writing to output file, %s. Error: %s\n", outputFilePath, strerror(errno));
               return errno;
           };
       }


       if(close(inputFile_fd) ); // close key file
   }
   closedir(inputDir_dfd); // close Dir

   assert(!close(key_fd)); // close key file

}

thanks.

  • 1
    Have you included `stdio.h?`. What is your code? – Linus Oct 30 '15 at 18:07
  • Probably your linkage command line is missing a `-lc` for the C library at the end. – Jens Gustedt Oct 30 '15 at 18:12
  • Added ' -lc ' to build options in Clion, it didn't work. it does compile on my Kubuntu machine. It's just in Clion. – Eylon Saadon Oct 30 '15 at 18:17
  • @EylonSaadon I assume you have `glibc` on your system right? – Linus Oct 30 '15 at 18:19
  • @Linus not sure what 'glibc' is. but from searching for it I found that MinGW does not build against glibc, it builds against msvcrt. As such, it uses libmsvcrtXX.a instead.http://stackoverflow.com/questions/6394512/standard-c-library-in-mingw – Eylon Saadon Oct 30 '15 at 18:31

0 Answers0