g++ compiler and makefile issues

A forum for learning, discussing, developing, and identifying top quality science materials -- physics, chemistry, mathematics, engineering, computer science, geology, biology, etc. Being versant in these areas will help make one versant in exploration of ID and Creation Science.

g++ compiler and makefile issues

Postby stcordova » Wed Feb 18, 2015 7:07 pm

I'm having problems compiling a makefile pertaining to DNA processing. The first round is to try to isolate the error at its base. I was able to duplicate the essentials of the error using the following hello.c program:

Code: Select all
#include <stdio.h>
main(){
printf("hello world\n");
int test = atoi("5");
printf("test: %d\n",test);
}



Here is the dialogue to compile and run using the "cc" compiler:

Code: Select all
[chattooga]$ rm hello
[chattooga]$ rm a.out
[chattooga]$ cc hello.c
[chattooga]$ ./a.out
hello world
test: 5
[chattooga]$ make hello
cc   hello.o   -o hello
[chattooga]$ ./hello
hello world
test: 5
[chattooga]$


not bad, but then:

Code: Select all
[chattooga]$ g++ hello.c
hello.c: In function 'int main()':
hello.c:4:20: error: 'atoi' was not declared in this scope
[chattooga]$ gcc hello.c
[chattooga]$ ./a.out
hello world
test: 5
[chattooga]$


Ok some config data on my VPS system:

Code: Select all
[chattooga]$ clear
[chattooga]$ echo $PATH
/home/stcordova2/.gems/bin:/usr/lib/ruby/gems/1.8/bin/:/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games
[chattooga]$
[chattooga]$
[chattooga]$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
[chattooga]$
[chattooga]$
[chattooga]$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
[chattooga]$
[chattooga]$
[chattooga]$ cc -v
Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.3-1ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
[chattooga]$
stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am

Re: g++ compiler and makefile issues

Postby WinstonEwert » Wed Feb 18, 2015 9:39 pm

Sal,

atoi() comes from stdlib.h not stdio.h.

stdio.h may #include stdlib.h or not depending on the compiler.

It looks like what's happening is that the
Code: Select all
cc
compiler includes stdlib.h in stdio.h but the g++ compiler does not.

The solution is to add a line
Code: Select all
#include <stdlib.h>


Its a common issue when compiling older code or code from a different platform.
WinstonEwert
 
Posts: 17
Joined: Thu Mar 06, 2014 12:01 am

Re: g++ compiler and makefile issues

Postby stcordova » Thu Feb 19, 2015 12:44 am

Winston,

WOW! Thank you! Although I hate modifying source code provided by a 3rd party, I added the line you suggested (you'll see the comment identifying the modification in the source below):

Code: Select all
/*------------------------------------------------------------------------------
//   Programmer: james robert white, The Center for Bioinformatics and
//               Computational Biology (CBCB)               
//         File: kmerdist.cc
//         Date: 06/25/07
//
//        Input: Input is a single multi-fasta sequence file, an output name, a
//        word length K, a window length L, an end length E, and M bp as
//               the beginning of the safe zone (E > M).
//
//       Output: Output is to prefix.kmers, and it is the frequency tables
//               of each kmer in bins of length L up to base E.  Also, the gc
//               content of the reads is outputted as prefix.gc.
//
//        Usage: kmerdist <fasta reads> <prefix.kmers> <K> <L> <E> <M> <prefix.gc>
-------------------------------------------------------------------------------*/
#include <stdlib.h> //STCORDOVA MOD

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
#include <map>
#include <sstream>

using namespace std;

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   
%%  Initialize Global variables  %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
//map<string, short int> Nkmers;
unsigned long int kmerBins[65536][100];
int numBins = 0;
int K = 0;
int L = 0;
int E = 0;
int M = 0;

double GC = 0;
double bp = 0;
double GCcontent = 0;

/*%%%%%%%%%%%%%%%%%   
%%  Subroutines  %%
%%%%%%%%%%%%%%%%%%*/

int kmer2Int(string *str)
{
  // ok, we need to break down this word letter
  // by letter and return an integer that reps
  // the whole word uniquely 

  // we let A = 0, C = 1 (01), G = 2 (10), and T = 3 (11)
  // and use bit operations to create a 16 bit number to
  // represent our word
  string kmer = *str;
  unsigned short int returnint = 0;

  for (int i = 0; i < K ; i+= 1){
    char s = toupper(kmer.at(i));     
    if       (s == 'A'){
      returnint = returnint | 0;
    }else if (s == 'C'){
      returnint = returnint | 1;
    }else if (s == 'G'){
      returnint = returnint | 2;
    }else if (s == 'T'){
      returnint = returnint | 3;
    }else if (s == 'N'){
      // then this word has an N in it, so we'll keep it in the list
      return -1; 
    }   
   
    if (i < (K-1)){
      returnint = returnint << 2;  // shift the number by two bits
    }
  }

  return (returnint);
}


string int2Kmer(unsigned short int i)
{
  // ok, we need to decode this number
  // into its corresponding kmer

  // we let A = 0, C = 1 (01), G = 2 (10), and T = 3 (11)
  // and use bit operations to create a 16 bit number to
  // represent our word
  string kmer = "";
  int subint = 0;

  for (int j = 0; j < K ; j += 1){
    subint = i & 3;
    if       (subint == 0){
      kmer =  'A' + kmer;
    }else if (subint == 1){
      kmer = 'C' + kmer;
    }else if (subint == 2){
      kmer = 'G' + kmer;
    }else if (subint == 3){
      kmer = 'T' + kmer;
    }

    i = i >> 2;  // shift the number by two bits!
  }

  return (kmer);
}


void countKmers(string *str)
{
  string line = *str;
  int linelength = line.length();

  //for each kmer, record its occurrence
  for (int i = 0 ; i < (linelength-K+1) ; i++){
    string kmer = line.substr(i, K); 
    string *kmerptr = &kmer;

    int bin = int(floor(double(i/L)));
   
    if (i >= E){
      break;
    }       

    int intkmer = kmer2Int(kmerptr);   
    if (intkmer == -1){
  //  Nkmers.insert(make_pair(kmer, 0));       
      continue;
    }else{
      kmerBins[intkmer][bin]++;
    }
  }


  //Count Gs and Cs and all.
  for (int i = (M+1) ; i < linelength ; i++){
    char s = toupper(line.at(i));
    bp++;
    if (s == 'G' || s == 'C'){
      GC++;
    }
  }

}



// find the name of the read
// no name can be more than 40 chars long!
string findName(string *str)
{
  string header = "";
  string line = *str; 
  int linelength = line.length();

  for (int i = 0; i < linelength; i++){
    char s = line.at(i);
 
    if (s == ' '){
      //then return from index 1 to i-1
      header = line.substr(1, i-1);
      return(header); 
    }     
  }     

  header = line.substr(1,linelength);
  return(header);
 
}


void  buildKmerFreqTables(ifstream & in)
{
 
  static string current = "";
  string line = ""; string *currentptr;
  currentptr = &line;

  string out = "";  string *outptr = &out;
  string name = "";
  int check = 0;
 
  //open the multiple fasta file and let's get started
  while (getline(in, line)){
    current = line;   
   
    //designed for the first read
    if (line[0] == '>' && check == 0){
      name  = findName(currentptr);
      check = 1;
      continue;
    } //this is the ID name we need


    //for all other reads
    if (line[0] == '>' && check == 1){
      countKmers(outptr); 
 
      //reset vars
      out = "";
      name  = findName(currentptr);
      continue;
    } //this is the ID name we need

    out += line;
  }

  countKmers(outptr);

}


void printOutFreqTables(ofstream & out)
{
  // we need to access each string element and bin
  // and print them out accordingly.
  // first we need to access each kmer independently

  // print out any kmer that occurred containing an 'N'
  /*map<string, short int>:: iterator p;

  p = Nkmers.begin() ;
  while(p != Nkmers.end()){
    out << p->first << "\n";
    p++;
  }
  */

  //print out all kmer freqs
  for (int i = 0; i < 65536; i+= 1){
    string kmer = int2Kmer(i);

    int check = 0;
    for (int j = 0; j < numBins ; j+= 1){
      if (kmerBins[i][j] > 0){
        check = 1;
        break;
      }
    }

    if (check == 1){ //then this mer occurs at least once
      out << kmer;

      for (int j = 0; j < numBins ; j+= 1){
        out << " " << kmerBins[i][j];
      }
      out << "\n";
    }
 
    if (i == 65535){
      break;
    }
  }

}




/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%  MAIN   %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
int main(int argc, char *argv[])
{

  //how many bins are we going to need?
  //numBins = EndofSZ/Length
  M = atoi (argv[6]);
  E = atoi (argv[5]);
  L = atoi (argv[4]);
  K = atoi (argv[3]);
  numBins = E/L;


  cout << "Initializing array ... \n";

  //initialize bins array
  //unsigned long int kmerBins[65536][25];
  for (int i = 0 ; i < 65536 ; i+= 1){
    for (int j = 0; j < numBins ; j+= 1){
       kmerBins[i][j] = 0;
    }   
  }

  //check to make sure it will open
  ifstream in(argv[1]);
  if(!in){
    cout << "Cannot open the file.\n";
    return 1;
  }

  cout << "Building frequency table ... \n";
  buildKmerFreqTables(in);
 

  cout << "Printing out frequencies ... \n";
  ofstream out(argv[2]);
  printOutFreqTables(out);
  out.close();

  //handle gc content
  ofstream out2(argv[7]);
  GCcontent = GC/bp;
  out2 << GCcontent << "\n";
  out2.close();
 

}



stcordova
 
Posts: 447
Joined: Wed Mar 05, 2014 1:41 am


Return to Science, Engineering, Mathematics

cron