int to size_t

Discussion in 'C++' started by Gary Wessle, Jul 21, 2006.

  1. Gary Wessle

    Gary Wessle Guest

    Hi

    in the gsl, I am trying to use a function but getting this error

    ****************************************************************
    read_data.o: In function `read_data::matrix_the_file()':
    read_data.cpp:(.text+0x4e): undefined reference to `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
    read_data.cpp:(.text+0x8c): undefined reference to `gsl_matrix_fscanf'
    Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
    ****************************************************************


    the docs/example say
    ****************************************************************
    This function creates a matrix of size n1 rows by n2 columns,
    returning a pointer to a newly initialized matrix struct. A new
    block is allocated for the elements of the matrix, and stored in
    the block component of the matrix struct. The block is owned by
    the matrix, and will be deallocated when the matrix is
    deallocated.
    example given:
    gsl_matrix * m = gsl_matrix_alloc (10, 3);
    ****************************************************************

    my code
    **************** file.h ****************
    class read_data
    {
    int nRows, nCol;
    void No_of_Rows_Cols();
    gsl_matrix * m;
    gsl_matrix * gsl_matrix_alloc(size_t, size_t);
    void matrix_the_file();

    ....
    };

    **************** file.cpp ****************
    ....
    void read_data::matrix_the_file(){
    // allocate memory for the matrix
    // needs to be freed later using
    // void gsl_matrix_free (gsl_matrix * m)
    m = gsl_matrix_alloc (nRows, nCol); //<<------- here
    FILE * f = fopen(file_name.c_str(), "rb");
    gsl_matrix_fscanf (f, m);
    fclose(f);

    }
    ....
    ****************************************************************
    how can I case an int into size_t?

    thanks
     
    Gary Wessle, Jul 21, 2006
    #1
    1. Advertising

  2. Gary Wessle wrote:
    > in the gsl,


    I don't know what "gsl" is.

    > I am trying to use a function but getting this error
    >
    > ****************************************************************
    > read_data.o: In function `read_data::matrix_the_file()':
    > read_data.cpp:(.text+0x4e): undefined reference to
    > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
    > read_data.cpp:(.text+0x8c): undefined reference to
    > `gsl_matrix_fscanf'
    > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
    > ****************************************************************


    Undefined reference is usually a _linker_ error. Have you provided
    the definition for those function to the linker?

    > the docs/example say
    > ****************************************************************
    > This function creates a matrix of size n1 rows by n2 columns,
    > returning a pointer to a newly initialized matrix struct. A new
    > block is allocated for the elements of the matrix, and stored in
    > the block component of the matrix struct. The block is owned by
    > the matrix, and will be deallocated when the matrix is
    > deallocated.
    > example given:
    > gsl_matrix * m = gsl_matrix_alloc (10, 3);
    > ****************************************************************
    >
    > my code
    > **************** file.h ****************
    > class read_data
    > {
    > int nRows, nCol;
    > void No_of_Rows_Cols();
    > gsl_matrix * m;
    > gsl_matrix * gsl_matrix_alloc(size_t, size_t);


    This is the declaration of that function. Where is the definition?

    > void matrix_the_file();
    >
    > ...
    > };
    >
    > **************** file.cpp ****************
    > ...
    > void read_data::matrix_the_file(){
    > // allocate memory for the matrix
    > // needs to be freed later using
    > // void gsl_matrix_free (gsl_matrix * m)
    > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
    > FILE * f = fopen(file_name.c_str(), "rb");
    > gsl_matrix_fscanf (f, m);
    > fclose(f);
    >
    > }
    > ...
    > ****************************************************************
    > how can I case an int into size_t?


    This has nothing to do with "casing" of an int into size_t. The linker
    cannot find the definition of that function. Make sure you tell it where
    to find the definition.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 21, 2006
    #2
    1. Advertising

  3. Gary Wessle

    Gary Wessle Guest

    "Victor Bazarov" <> writes:

    > Gary Wessle wrote:
    > > in the gsl,

    >
    > I don't know what "gsl" is.
    >
    > > I am trying to use a function but getting this error
    > >
    > > ****************************************************************
    > > read_data.o: In function `read_data::matrix_the_file()':
    > > read_data.cpp:(.text+0x4e): undefined reference to
    > > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
    > > read_data.cpp:(.text+0x8c): undefined reference to
    > > `gsl_matrix_fscanf'
    > > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
    > > ****************************************************************

    >
    > Undefined reference is usually a _linker_ error. Have you provided
    > the definition for those function to the linker?
    >
    > > the docs/example say
    > > ****************************************************************
    > > This function creates a matrix of size n1 rows by n2 columns,
    > > returning a pointer to a newly initialized matrix struct. A new
    > > block is allocated for the elements of the matrix, and stored in
    > > the block component of the matrix struct. The block is owned by
    > > the matrix, and will be deallocated when the matrix is
    > > deallocated.
    > > example given:
    > > gsl_matrix * m = gsl_matrix_alloc (10, 3);
    > > ****************************************************************
    > >
    > > my code
    > > **************** file.h ****************
    > > class read_data
    > > {
    > > int nRows, nCol;
    > > void No_of_Rows_Cols();
    > > gsl_matrix * m;
    > > gsl_matrix * gsl_matrix_alloc(size_t, size_t);

    >
    > This is the declaration of that function. Where is the definition?
    >
    > > void matrix_the_file();
    > >
    > > ...
    > > };
    > >
    > > **************** file.cpp ****************
    > > ...
    > > void read_data::matrix_the_file(){
    > > // allocate memory for the matrix
    > > // needs to be freed later using
    > > // void gsl_matrix_free (gsl_matrix * m)
    > > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
    > > FILE * f = fopen(file_name.c_str(), "rb");
    > > gsl_matrix_fscanf (f, m);
    > > fclose(f);
    > >
    > > }
    > > ...
    > > ****************************************************************
    > > how can I case an int into size_t?

    >
    > This has nothing to do with "casing" of an int into size_t. The linker
    > cannot find the definition of that function. Make sure you tell it where
    > to find the definition.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    I have in my header file
    #include "gsl/gsl_matrix.h"

    I then created a symlink to the directory where the library is.
    $ ln -sf /usr/include/gsl/ myPrograms/common/

    :~$ ls -l myPrograms/common/
    total 32
    lrwxrwxrwx 1 fred fred 17 2006-07-22 07:34 gsl -> /usr/include/gsl/
    -rw-r--r-- 1 fred fred 234 2006-07-20 14:07 makefile
    -rw-r-xr-- 1 fred fred 67 2006-07-21 11:57 #my.cpp#
    -rw-r--r-- 1 fred fred 1000 2006-07-22 03:40 read_data.cpp
    -rw-r--r-- 1 fred fred 379 2006-07-22 07:48 read_data.h
    -rw-r--r-- 1 fred fred 4860 2006-07-22 07:35 read_data.o
    -rw-r--r-- 1 fred fred 216 2006-07-21 19:56 read_data_test.cpp
    -rw-r--r-- 1 fred fred 2380 2006-07-21 20:41 read_data_test.o

    all this to avoid
    1) add it to my compiler search path.
    2) provide its location to the preprocessor in the makefile below

    since I don't know how to do either.
     
    Gary Wessle, Jul 21, 2006
    #3
  4. "Gary Wessle" <> wrote in message
    news:...
    > "Victor Bazarov" <> writes:
    >
    >> Gary Wessle wrote:
    >> > in the gsl,

    >>
    >> I don't know what "gsl" is.
    >>
    >> > I am trying to use a function but getting this error
    >> >
    >> > ****************************************************************
    >> > read_data.o: In function `read_data::matrix_the_file()':
    >> > read_data.cpp:(.text+0x4e): undefined reference to
    >> > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
    >> > read_data.cpp:(.text+0x8c): undefined reference to
    >> > `gsl_matrix_fscanf'
    >> > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
    >> > ****************************************************************

    >>
    >> Undefined reference is usually a _linker_ error. Have you provided
    >> the definition for those function to the linker?
    >>
    >> > the docs/example say
    >> > ****************************************************************
    >> > This function creates a matrix of size n1 rows by n2 columns,
    >> > returning a pointer to a newly initialized matrix struct. A new
    >> > block is allocated for the elements of the matrix, and stored in
    >> > the block component of the matrix struct. The block is owned by
    >> > the matrix, and will be deallocated when the matrix is
    >> > deallocated.
    >> > example given:
    >> > gsl_matrix * m = gsl_matrix_alloc (10, 3);
    >> > ****************************************************************
    >> >
    >> > my code
    >> > **************** file.h ****************
    >> > class read_data
    >> > {
    >> > int nRows, nCol;
    >> > void No_of_Rows_Cols();
    >> > gsl_matrix * m;
    >> > gsl_matrix * gsl_matrix_alloc(size_t, size_t);

    >>
    >> This is the declaration of that function. Where is the definition?
    >>
    >> > void matrix_the_file();
    >> >
    >> > ...
    >> > };
    >> >
    >> > **************** file.cpp ****************
    >> > ...
    >> > void read_data::matrix_the_file(){
    >> > // allocate memory for the matrix
    >> > // needs to be freed later using
    >> > // void gsl_matrix_free (gsl_matrix * m)
    >> > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
    >> > FILE * f = fopen(file_name.c_str(), "rb");
    >> > gsl_matrix_fscanf (f, m);
    >> > fclose(f);
    >> >
    >> > }
    >> > ...
    >> > ****************************************************************
    >> > how can I case an int into size_t?

    >>
    >> This has nothing to do with "casing" of an int into size_t. The linker
    >> cannot find the definition of that function. Make sure you tell it where
    >> to find the definition.
    >>
    >> V
    >> --
    >> Please remove capital 'A's when replying by e-mail
    >> I do not respond to top-posted replies, please don't ask

    >
    > I have in my header file
    > #include "gsl/gsl_matrix.h"
    >
    > I then created a symlink to the directory where the library is.
    > $ ln -sf /usr/include/gsl/ myPrograms/common/
    >
    > :~$ ls -l myPrograms/common/
    > total 32
    > lrwxrwxrwx 1 fred fred 17 2006-07-22 07:34 gsl -> /usr/include/gsl/
    > -rw-r--r-- 1 fred fred 234 2006-07-20 14:07 makefile
    > -rw-r-xr-- 1 fred fred 67 2006-07-21 11:57 #my.cpp#
    > -rw-r--r-- 1 fred fred 1000 2006-07-22 03:40 read_data.cpp
    > -rw-r--r-- 1 fred fred 379 2006-07-22 07:48 read_data.h
    > -rw-r--r-- 1 fred fred 4860 2006-07-22 07:35 read_data.o
    > -rw-r--r-- 1 fred fred 216 2006-07-21 19:56 read_data_test.cpp
    > -rw-r--r-- 1 fred fred 2380 2006-07-21 20:41 read_data_test.o
    >
    > all this to avoid
    > 1) add it to my compiler search path.
    > 2) provide its location to the preprocessor in the makefile below
    >
    > since I don't know how to do either.


    I don't know what all that lrwxrwxrwx stuff is, but it looks like you
    are trying to call a method (gsl_matrix_alloc) defined in some
    library. But the way you've coded it you're trying to call a member
    in read_data with the same name.

    I'd try taking the declaration for gsl_matrix_alloc out of read_data.
     
    Michael Taylor, Jul 21, 2006
    #4
  5. Gary Wessle

    Gary Wessle Guest

    "Michael Taylor" <> writes:

    > "Gary Wessle" <> wrote in message
    > news:...
    > > "Victor Bazarov" <> writes:
    > >
    > >> Gary Wessle wrote:
    > >> > in the gsl,
    > >>
    > >> I don't know what "gsl" is.
    > >>
    > >> > I am trying to use a function but getting this error
    > >> >
    > >> > ****************************************************************
    > >> > read_data.o: In function `read_data::matrix_the_file()':
    > >> > read_data.cpp:(.text+0x4e): undefined reference to
    > >> > `read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
    > >> > read_data.cpp:(.text+0x8c): undefined reference to
    > >> > `gsl_matrix_fscanf'
    > >> > Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
    > >> > ****************************************************************
    > >>
    > >> Undefined reference is usually a _linker_ error. Have you provided
    > >> the definition for those function to the linker?
    > >>
    > >> > the docs/example say
    > >> > ****************************************************************
    > >> > This function creates a matrix of size n1 rows by n2 columns,
    > >> > returning a pointer to a newly initialized matrix struct. A new
    > >> > block is allocated for the elements of the matrix, and stored in
    > >> > the block component of the matrix struct. The block is owned by
    > >> > the matrix, and will be deallocated when the matrix is
    > >> > deallocated.
    > >> > example given:
    > >> > gsl_matrix * m = gsl_matrix_alloc (10, 3);
    > >> > ****************************************************************
    > >> >
    > >> > my code
    > >> > **************** file.h ****************
    > >> > class read_data
    > >> > {
    > >> > int nRows, nCol;
    > >> > void No_of_Rows_Cols();
    > >> > gsl_matrix * m;
    > >> > gsl_matrix * gsl_matrix_alloc(size_t, size_t);
    > >>
    > >> This is the declaration of that function. Where is the definition?
    > >>
    > >> > void matrix_the_file();
    > >> >
    > >> > ...
    > >> > };
    > >> >
    > >> > **************** file.cpp ****************
    > >> > ...
    > >> > void read_data::matrix_the_file(){
    > >> > // allocate memory for the matrix
    > >> > // needs to be freed later using
    > >> > // void gsl_matrix_free (gsl_matrix * m)
    > >> > m = gsl_matrix_alloc (nRows, nCol); //<<------- here
    > >> > FILE * f = fopen(file_name.c_str(), "rb");
    > >> > gsl_matrix_fscanf (f, m);
    > >> > fclose(f);
    > >> >
    > >> > }
    > >> > ...
    > >> > ****************************************************************
    > >> > how can I case an int into size_t?
    > >>
    > >> This has nothing to do with "casing" of an int into size_t. The linker
    > >> cannot find the definition of that function. Make sure you tell it where
    > >> to find the definition.
    > >>
    > >> V
    > >> --
    > >> Please remove capital 'A's when replying by e-mail
    > >> I do not respond to top-posted replies, please don't ask

    > >
    > > I have in my header file
    > > #include "gsl/gsl_matrix.h"
    > >
    > > I then created a symlink to the directory where the library is.
    > > $ ln -sf /usr/include/gsl/ myPrograms/common/
    > >
    > > :~$ ls -l myPrograms/common/
    > > total 32
    > > lrwxrwxrwx 1 fred fred 17 2006-07-22 07:34 gsl -> /usr/include/gsl/
    > > -rw-r--r-- 1 fred fred 234 2006-07-20 14:07 makefile
    > > -rw-r-xr-- 1 fred fred 67 2006-07-21 11:57 #my.cpp#
    > > -rw-r--r-- 1 fred fred 1000 2006-07-22 03:40 read_data.cpp
    > > -rw-r--r-- 1 fred fred 379 2006-07-22 07:48 read_data.h
    > > -rw-r--r-- 1 fred fred 4860 2006-07-22 07:35 read_data.o
    > > -rw-r--r-- 1 fred fred 216 2006-07-21 19:56 read_data_test.cpp
    > > -rw-r--r-- 1 fred fred 2380 2006-07-21 20:41 read_data_test.o
    > >
    > > all this to avoid
    > > 1) add it to my compiler search path.
    > > 2) provide its location to the preprocessor in the makefile below
    > >
    > > since I don't know how to do either.

    >
    > I don't know what all that lrwxrwxrwx stuff is, but it looks like you
    > are trying to call a method (gsl_matrix_alloc) defined in some
    > library. But the way you've coded it you're trying to call a member
    > in read_data with the same name.
    >
    > I'd try taking the declaration for gsl_matrix_alloc out of read_data.



    I am getting
    ****************************************************************
    fred@debian:~/myPrograms/common$ make
    g++ -c -o read_data.o read_data.cpp
    g++ read_data.o read_data_test.o -o proj
    read_data.o: In function `read_data::matrix_the_file()':
    read_data.cpp:(.text+0x47): undefined reference to `gsl_matrix_alloc'
    read_data.cpp:(.text+0x85): undefined reference to `gsl_matrix_fscanf'
    collect2: ld returned 1 exit status
    make: *** [proj] Error 1
    fred@debian:~/myPrograms/common$
    ****************************************************************

    the files are

    **************** read_data.h ****************
    #ifndef READ_DATA_H
    #define READ_DATA_H
    #include <string>
    #include <cstdio>
    #include "gsl/gsl_matrix.h"
    class read_data
    {
    int nRows, nCol;
    std::string file_name;
    void No_of_Rows_Cols();
    gsl_matrix * m;
    void matrix_the_file();

    public:
    read_data(std::string const& fileName);
    ~read_data();

    };


    #endif



    **************** read_data.cpp ****************
    #include <string>
    #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <cstdlib>
    #include <cstdio>
    #include "gsl/gsl_matrix.h"
    #include "read_data.h"

    using namespace std;

    read_data::read_data( string const& fileName )
    : file_name( fileName ) {
    set_No_of_Rows_Cols();
    matrix_the_file();
    }

    read_data::~read_data() {}

    void read_data::matrix_the_file(){
    // allocate memory for the matrix
    // needs to be freed later using
    // void gsl_matrix_free (gsl_matrix * m)
    m = gsl_matrix_alloc (nRows, nCol);
    FILE * f = fopen(file_name.c_str(), "rb");
    gsl_matrix_fscanf (f, m);
    fclose(f);

    }

    void read_data::set_No_of_Rows_Cols() {
    ifstream in(file_name.c_str());
    string line;
    getline(in, line);
    stringstream input( line.c_str() );

    string word;
    nCol = 0;
    while(input >> word)
    nCol++;

    nRows = 1;
    while (getline(in, line))
    nRows++;
    }

    /*
    another solution to use system fun. output
    std::string command = "wc -l";
    std::system( ( command + " " + file_name ).c_str() );
    }
    */


    **************** read_data_test.cpp ****************
    #include <fstream>
    #include <iostream>
    #include <string>
    #include "read_data.h"

    using namespace std;

    int main() {
    string f = "../../data/ZB/Jun06/20060405";
    read_data data1( f ); // space delimited


    }



    **************** makefile ****************
    OBJS = read_data.o read_data_test.o
    COMP = g++

    #### linker section ####
    proj: $(OBJS)
    $(COMP) $(OBJS) -o proj
    ########################


    #### compiler section ####
    ..SUFFIXES:.o .cpp .h
    ..h.o:
    $(COMP) -c $<
    ##########################
     
    Gary Wessle, Jul 22, 2006
    #5
  6. Gary Wessle wrote:
    > [...]
    > I am getting
    > ****************************************************************
    > fred@debian:~/myPrograms/common$ make
    > g++ -c -o read_data.o read_data.cpp
    > g++ read_data.o read_data_test.o -o proj
    > read_data.o: In function `read_data::matrix_the_file()':
    > read_data.cpp:(.text+0x47): undefined reference to `gsl_matrix_alloc'


    OK, let's just take this one function, 'gsl_matrix_alloc'.

    > read_data.cpp:(.text+0x85): undefined reference to `gsl_matrix_fscanf'
    > collect2: ld returned 1 exit status
    > make: *** [proj] Error 1
    > fred@debian:~/myPrograms/common$
    > ****************************************************************
    >
    > the files are
    > [...]
    >
    > **************** read_data.cpp ****************
    > [...]
    > void read_data::matrix_the_file(){
    > // allocate memory for the matrix
    > // needs to be freed later using
    > // void gsl_matrix_free (gsl_matrix * m)
    > m = gsl_matrix_alloc (nRows, nCol);


    I see it *used* here...

    > FILE * f = fopen(file_name.c_str(), "rb");
    > gsl_matrix_fscanf (f, m);
    > fclose(f);
    >
    > }
    > [...]


    But not *defined* anywhere in _your_ program. Why? Becuase, as you might
    have mentioned, it comes from a *library*. You need to supply that library
    to your *linker* at the link stage. Learn how to do it (because it's OT
    here to discuss any particular compiler's command-line options).

    A hint: the command line you supply

    > g++ read_data.o read_data_test.o -o proj


    is probably missing something. It seems to have no libraries at all, only
    your own object files.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jul 24, 2006
    #6

  7. >>>>>I am trying to use a function but getting this error
    >>>>>
    >>>>>****************************************************************
    >>>>>read_data.o: In function `read_data::matrix_the_file()':
    >>>>>read_data.cpp:(.text+0x4e): undefined reference to
    >>>>>`read_data::gsl_matrix_alloc(unsigned int, unsigned int)'
    >>>>>read_data.cpp:(.text+0x8c): undefined reference to
    >>>>>`gsl_matrix_fscanf'
    >>>>>Function: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
    >>>>>****************************************************************
    >>>>
    >>>>Undefined reference is usually a _linker_ error. Have you provided
    >>>>the definition for those function to the linker?
    >>>>
    >>>


    > [...]


    This looks like a linker error -- you told your compiler
    you have a gsl_matrix_alloc function, but forgot to tell it
    where to find it (its definition).

    As Victor already pointed out, learning how to operate
    your compiler/linker is beyond the scope of this group,
    try your man pages (<OT> hint: look for the options -l and -L </OT>).


    >>>1) add it to my compiler search path.
    >>>2) provide its location to the preprocessor in the makefile below
    >>>
    >>>since I don't know how to do either.


    So it seems. <eg>

    > [snip]


    HTH,
    - J.
     
    Jacek Dziedzic, Jul 24, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,237
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,686
    Old Wolf
    Jan 20, 2004
  3. Replies:
    6
    Views:
    6,446
    Greg Comeau
    Oct 19, 2005
  4. Tim Prince

    Re: int vs. unsigned int vs. size_t

    Tim Prince, Apr 30, 2011, in forum: C Programming
    Replies:
    3
    Views:
    294
    Ian Collins
    Apr 30, 2011
  5. Ian Collins

    Re: int vs. unsigned int vs. size_t

    Ian Collins, Apr 30, 2011, in forum: C Programming
    Replies:
    2
    Views:
    342
    Ian Collins
    Apr 30, 2011
Loading...

Share This Page