When compiling the hadoop program specified in the question using the command
g++ /export/hadoop-1.0.1/src/c++ -Iinstall/include -Linstall/lib -lhadooputils -lhadooppipes -lssl -lcrypto -lpthread -lsocket -lnsl -lpq -lsrc -o parindex.
As a result, an error occurred.
Undefined first referenced symbol in file Traverser(char const*) /var/tmp//ccdqaaGF.o ld: fatal: symbol referencing errors. No output written to parindex
collect2: ld returned 1 exit status
I connected the -lsrc
library with the -lsrc
key, which was made from the files avl_tree.c, buf_read.c, db_prep.c, file_process.c, global_header.c, traverser.c
.
I give the text of the file traverser.c
:
#include <unistd.h> #include <sys/stat.h> #include <fcntl.h> #include <pwd.h> #include <sys/types.h> #include <time.h> #include "include/traverser.h" const char * pParams[] = { "ServerIP", "ServerPort", "DBName", "Login", "Password", NULL }; char sIP[15]; char sPort[5]; char sDBName[64]; char sLogin[64]; char sPassword[64]; char sConnInfo[512]; int nClock = 0; long int TraverseFiles(PGconn *pConn, const char * sDir); long int TraverseDirs(PGconn *pConn, const char * sDir); long int CheckParam(const char * sParam) { long int i = 0; for (; pParams[i]; ++i) if (!strcmp(sParam, pParams[i])) return i; return -1; } long int Configure(const char * sConfig) { char sParam[64]; long int nParamID; long int ret = 0;; char sValue[64]; char sError[512]; FILE * pConfig = fopen(sConfig, "r"); for (;;) { memset(sParam, 0, 64); fscanf(pConfig, "%64[^=]=", sParam); if (!sParam[0]) break; nParamID = CheckParam(sParam); if (nParamID == -1) { sprintf(sError, "Config error : %512s symbol not found\n"); AddErrorToLog(sError); ret = 1; break; } switch (nParamID) { case 0: fscanf(pConfig, "%15[^\n]\n", sIP); break; case 1: fscanf(pConfig, "%5[^\n]\n", sPort); break; case 2: fscanf(pConfig, "%64[^\n]\n", sDBName); break; case 3: fscanf(pConfig, "%64[^\n]\n", sLogin); break; case 4: fscanf(pConfig, "%64[^\n]\n", sPassword); break; default: AddErrorToLog("+++"); break; } } fclose(pConfig); return 0; } long int _Traverser(PGconn * pConn, const char * sTraversingRoot) { return TraverseFiles(pConn, sTraversingRoot) + TraverseDirs(pConn, sTraversingRoot); } void GetActualPath(char * sActualPath, const char * sTraversingRoot) { int p, l = 1, n = 0; char c; sActualPath[0] = '\0'; if (sTraversingRoot[0] != '/') { getcwd(sActualPath, 512); strcat(sActualPath, "/"); } strcat(sActualPath, sTraversingRoot); for (;;) { c = sActualPath[l]; switch(c) { case '.': ++n; break; case '\0': case '/': if (n == 0) { strcpy(sActualPath + l - 1, sActualPath + l); --l; } if (n == 1) { p = l--; while (sActualPath[l--] != '/') {} strcpy(sActualPath + ++l, sActualPath + p); } if (n == 2) { p = l; l -= 4; if (l < 0) { c = '\0'; sActualPath[0] = '\0'; break; } while (sActualPath[l--] != '/') {} strcpy(sActualPath + ++l, sActualPath + p); } n = 0; break; default: n = -1000; } ++l; if (c == '\0') break; } } long int Traverser(const char * sTraversingRoot) { long int nCount; struct stat rStatBuf; time_t nTime; char sActualPath[512]; PGconn *pConn; // Open DB connection sprintf(sConnInfo, "hostaddr=%s port=%s connect_timeout=50 dbname=%s user=%s password=%s", sIP, sPort, sDBName, sLogin, sPassword); pConn = PQconnectdb(sConnInfo); if (PQstatus(pConn) == CONNECTION_BAD) { AddErrorToLog("No connect\n"); return 0; } GetActualPath(sActualPath, sTraversingRoot); stat(sActualPath, &rStatBuf); if (nClock) nTime = time(NULL); if(S_ISREG(rStatBuf.st_mode)) { nCount = 1; ProcessFile(pConn, sActualPath); } if(S_ISDIR(rStatBuf.st_mode)) { nCount = _Traverser(pConn, sActualPath); } if (nClock) fprintf(stdout, "Total time : %u second(s)\n", time(NULL) - nTime); // Close DB connection PQfinish(pConn); return nCount; } // Open DB connection and get all files to process long int TraverseFiles(PGconn *pConn, const char * sDir) { long int nCount = 0; DIR * pDir; struct dirent *pDirEntry; struct stat rStatBuf; // Open directory if ((pDir = opendir(sDir)) == NULL) { long int nError = errno; AddErrorToLog("\nTraverse_file Error:"); AddErrorToLog(sDir); AddErrorToLog("Directory can not be opened"); AddErrorToLog(strerror(nError)); return 0; } // Traverse it char sTempDir[512]; while (pDirEntry = readdir(pDir)) { snprintf (sTempDir, sizeof(sTempDir), "%s/%s", sDir, pDirEntry->d_name); stat(sTempDir, &rStatBuf); if (S_ISREG(rStatBuf.st_mode)) { if (ProcessFile(pConn, sTempDir)) nCount++; } } // And close closedir(pDir); return nCount; } long int TraverseDirs(PGconn *pConn, const char * sDir) { long int nCount = 0; DIR * pDir; struct dirent *pDirEntry; struct stat rStatBuf; // Open directory if ((pDir = opendir(sDir)) == NULL) { long int nError = errno; AddErrorToLog("\nTraverse_dirs Error:"); AddErrorToLog(sDir); AddErrorToLog("Directory can not be opened"); AddErrorToLog(strerror(nError)); return 0; } // Traverse it char sTempDir[512]; while (pDirEntry = readdir(pDir)) // . and .. is special dirs that we should not process if ((strcmp(".", pDirEntry->d_name)) && strcmp("..", pDirEntry->d_name)) { snprintf (sTempDir, sizeof(sTempDir), "%s/%s", sDir, pDirEntry->d_name); stat(sTempDir, &rStatBuf); if (S_ISDIR(rStatBuf.st_mode)) { nCount += _Traverser(pConn, sTempDir); } } // And close closedir(pDir); return nCount; }
I give the code traverser.h
#ifndef _TRAVERSER_H_ #define _TRAVERSER_H_ #include "global_header.h" #include "file_process.h" // Recurent traverse of file system tree with sTraversingRoot as a root long int Traverser(const char * sTraversingRoot); long int Configure(const char * sConfig); #endif