User forums > Using Code::Blocks

Can't use standard functions like fprintf when using linker to link libraries

(1/2) > >>

chris:
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]);
    }


--- End code ---

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

Miguel Gimenez:
How is array[] defined?

chris:
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;
}

--- End code ---

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.

Miguel Gimenez:
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.

PB:
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.

Navigation

[0] Message Index

[#] Next page

Go to full version