Re: int to size_t
"Michael Taylor" <mbtaylor@aol.com> writes:
"Gary Wessle" <phddas@yahoo.com> wrote in message
news:878xmmtyoi.fsf@localhost.localdomain...
"Victor Bazarov" <v.Abazarov@comAcast.net> 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 $<
##########################