Author Topic: Can't use standard functions like fprintf when using linker to link libraries  (Read 2357 times)

Offline chris

  • Single posting newcomer
  • *
  • Posts: 3
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:

Code
 

    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
« Last Edit: December 22, 2022, 12:38:59 pm by chris »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
How is array[] defined?

Offline chris

  • Single posting newcomer
  • *
  • Posts: 3
its define as an int. This was just an example code, but I'll include it all here:

Code
#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.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
The error you are getting is because you are reading (or writing) in non-accesible memory. This is usually because a NULL pointer is dereferenced, but there are more possible causes like reading an array out of bounds.

The code you posted looks fine (except for not calling neither fflush() nor fclose()); Run the Debug target of your project through the debugger to see where it breaks and why.

Offline PB

  • Multiple posting newcomer
  • *
  • Posts: 57
As Miguel already wrote, the code you posted cannot cause a crash as is.

Therefore, the problem must be how the project was set up.

I would try creating a new project with C::B project wizard (menu File/New/Project), using "Console application" and replacing the provided code with yours. If the application still crashes, there is likely something wrong with your compiler installation or its setup in C::B.

If it does not crash, the problem must be with your actual project. For example, using incompatible libraries (built by incompatible compiler or with incompatible settings or linking to a different CRT (UCRT vs MSVCRT)) or having a wrong project setting. Unlike with Matlab, with C an C++, binary compatibility can be an issue and one has to ensure the libraries he uses are compatible with his compiler.
« Last Edit: December 22, 2022, 02:16:44 pm by PB »

Offline cacb

  • Lives here!
  • ****
  • Posts: 536
This code works fine when I don't link any libraries.

That's a clue. Make sure those libraries are built with the compiler you are using.

Offline jordi

  • Multiple posting newcomer
  • *
  • Posts: 22
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.
Code
     #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;
     }


Offline chris

  • Single posting newcomer
  • *
  • Posts: 3
This code works fine when I don't link any libraries.

That's a clue. Make sure those libraries are built with the compiler you are using.

LOL thank you! I'm completely new to using libraries, and followed instructions from stackexchange on how to install the library. The isntructions I followed were for MS Visual Studio. The confusing part for me was that it worked nearly perfectly (for my purposes) in C::B, until it didnt!

I've switched to using VS for now, and it works and prints as you would expect.

Anyone got any good recommendations on resources about what a compiler is... Feeling very out of my depth.