Hi, I'm new to learning to code in C, migrating from MATLAB, so I'm new to using external libraries and things, so please bear with...
I've installed the gnu scientific library to my C:\ drive, and linked it to each codeblocks project following instructions found online
1. copy the .dll files into the project folder
2. project > build options > search directories > compiler , then included the file path C:\...\gsl_x64-windows\include [I've added the dots here for clarity, the full file path is included]
3. project > build options > linker settings , and added C:\...\gsl_x64-windows\lib\gslcblas.lib and C:\...\gsl.lib
The project works fine for the most part. I'm able to use gsl functions, and everything seems to work correctly. The problem occurs when using fprintf to save my data to a .txt file.
It's step 3 that causes the issue for me. I've tried different iterations of each of those steps, and the error only occurs when I've included the .lib files in linker settings.
Every project that I set up in this way has the same problem. The programme runs until it reaches the fprintf line, and then it exits with the return -1073741819 (0xC0000005), which from googling seems to do with permission/access.
The files will be created in the folder (by the fopen command) but nothing gets written in them.
The weirdest part (to me at least) is that I CAN use fprintf when printing a typed out string, but as soon as I use a placeholder:
for (i = 0 ; i < 5 ; i++ ){
fprintf(fTest, "%d\n", array[i]);
}
it no longer works and gives me that error.
I might be missing something obvious, or not... I'm just confused as to why fprintf doesn't work when I include linked libraries.
To clarify, I am able to use fprintf when I don't use linker settings to link libaries, and the results are fine and as expected. However, I then can't use the libraries I need as it comes up with a error "undefined reference to [gsl function name]"
Please can someone let me know what is going on.
Thanks!
EDIT: Forgot to mention, the problem seems to linger after clearing linker settings, unless I click build > clear. Then problem stops then
its define as an int. This was just an example code, but I'll include it all here:
#include <stdio.h>
#include <stdlib.h>
#include <gsl\gsl_matrix.h> /* this is to test whether I can use the library. When linked, I can. */
int main()
{
int array[5], i;
for ( i = 0 ; i < 5 ; i++ ){
array[i] = i;
}
FILE *fTest = fopen("test.txt", "w");
if ( fTest == NULL ){printf("Err"); return -1; }
for (i = 0 ; i < 5 ; i++ ){
fprintf(fTest, "%d\n", array[i]);
}
return 0;
}
This code works fine when I don't link any libraries. It prints the array as integers as expected. It also works fine with any other data type. I am sometimes able to use fwrite to write my results as binary .dat files, but I need text files for what I'm trying to do.
You are not using any gsl function in your code. I would remove the include.
Weird but maybe some lib you are including have redefined the printf symbol
Try the next code with the libraries included.
#include <stdio.h>
#include <gsl/gsl_vector.h>
int
main (void)
{
int i;
gsl_vector * v = gsl_vector_alloc (100);
for (i = 0; i < 100; i++)
{
gsl_vector_set (v, i, 1.23 + i);
}
{
FILE * f = fopen ("test.dat", "w");
gsl_vector_fprintf (f, v, "%.5g");
fclose (f);
}
return 0;
}