C++ visual studio 2015 LNK2001 and LNK2019 errors when I build (Windows 8)

Hello everybody, I am getting these external errors, which I think (but not entirely sure) are related to some libraries I included into my C++ work and the linker. The errors when I build my entire project are:

1>gsl.lib(rng.obj) : error LNK2019: unresolved external symbol _printf referenced in function _gsl_rng_print_state
1>gsl.lib(error.obj) : error LNK2019: unresolved external symbol ___iob_func referenced in function _gsl_error
1>gsl.lib(stream.obj) : error LNK2001: unresolved external symbol ___iob_func
1>cblas.lib(xerbla.obj) : error LNK2001: unresolved external symbol ___iob_func
1>gsl.lib(error.obj) : error LNK2019: unresolved external symbol _fprintf referenced in function _gsl_error
1>gsl.lib(stream.obj) : error LNK2001: unresolved external symbol _fprintf
1>cblas.lib(xerbla.obj) : error LNK2001: unresolved external symbol _fprintf
1>cblas.lib(xerbla.obj) : error LNK2019: unresolved external symbol _vfprintf referenced in function _cblas_xerbla
1>c:\users\pmbanugo\documents\visual studio 2015\Projects\MonteCarloDemo\Debug\MonteCarloDemo.exe : fatal error LNK1120: 4 unresolved externals

Gsl is a library that has statistical packages that can be added on to C++ work (distributions, mean, mode, random numbers). When I look at the errors I see some C syntax with printf and fprintf, which means I guess the include files for GSL are written in some C language. I the Properties -> C/C++ ->Additional Include Directories I have for gsl the path :C:\gsl\gsl_extracted\gsl\include and in Properties -> Linker -> Input I have in Additional Dependencies the path of the .lib files whihc came when I downloaded the windows version of GSL: C:\gsl\gsl_extracted\gsl\lib\cblas.lib and C:\gsl\gsl_extracted\gsl\lib\gsl.lib .

In relation to the code I am using I have a main source C++ (.cpp) file but that has no GSL in it; it only includes header files I created that contains GSL syntax:#include "MetropHasting.h" and #include "GibbsM.h".

I have include the header files code I included into my main source (.cpp) file below:

"MetropHasting.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef METROPHASTING_H
#define METROPHASTING_H
#include <iostream>
#include <cstdlib>
#include "chartdir.h"
#include <string>
#include <sstream>
#include <array>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <ctime>


int rmvnorm(const gsl_rng *r, const int n, const gsl_vector *mean, const gsl_matrix *var, gsl_vector *result);
double target(double x[2]);
void MH(const int n_sim, double * mat, double stdDev) {
	//in our case the sample is 2 dimensional
	int n = 2;
	//define standard deviation
	//double sd = 1.5;
	double sd = stdDev;
	//initial values are 0,0
	double can[2] = { 0,0 };
	double update[2], uni, accept;
	int i, j;
	//const gsl_rng_type * T;
	gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);/*Define random number t-Mersenne random twister*/
	//srand(time(0)); //added
    long seed = (long)time(NULL); /*Define a seed for r*-previous code was time(NULL)*/
	gsl_rng_set(r, seed); /*Initiate the random number generator with seed*/

	gsl_vector *mean = gsl_vector_alloc(n);
	gsl_matrix *var = gsl_matrix_alloc(n, n);
	gsl_vector *result = gsl_vector_alloc(n);
	//set up variance matrix
	gsl_matrix_set(var, 0, 0, pow(sd, 2));
	gsl_matrix_set(var, 1, 1, pow(sd, 2));
	gsl_matrix_set(var, 0, 1, 0);
	gsl_matrix_set(var, 1, 0, 0);
	//start Metropolis Hasting algorithm
	for (i = 0; i < n_sim; i++) {
		//set up the mean vector
		gsl_vector_set(mean, 0, can[0]);
		gsl_vector_set(mean, 1, can[1]);
		//block update, use multivariate normal random sample generator
		rmvnorm(r, n, mean, var, result);
		//store the sample
		for (j = 0; j < n; j++) {
			update[j] = gsl_vector_get(result, j);
		}
		/*
 now calculate the acceptance rate and see whether the sample is accepted or not
		*/
		uni = gsl_rng_uniform(r);
		accept = target(update) / target(can);

		if (uni < accept) {
			//if acceptance rate is large enough, accept the update
			can[0] = update[0];
			can[1] = update[1];
			mat[2 * i] = update[0];
			mat[2 * i + 1] = update[1];
			//printf("\%f\t\%f\n",can[0],can[1]);

		}
		else {
			//otherwise do not update
			mat[2 * i] = can[0];
			mat[2 * i + 1] = can[1];
			//printf("\%f\t\%f\n",can[0],can[1]);
		}

	}
	gsl_vector_free(result);
	gsl_vector_free(mean);
}

//random multivariate normal sample generator
int rmvnorm(const gsl_rng *r, const int n, const gsl_vector *mean, const gsl_matrix *var, gsl_vector *result) {
	/* multivariate normal distribution random number generator */
	/*
	*	n	dimension of the random vector
	*	mean	vector of means of size n
	*	(var) variance	variance matrix of dimension n x n
	*	result	output variable with a single random vector normal distribution generation
	*/
	int k;
	gsl_matrix *work = gsl_matrix_alloc(n, n);

	gsl_matrix_memcpy(work, var);
	gsl_linalg_cholesky_decomp(work);

	for (k = 0; k<n; k++)
		gsl_vector_set(result, k, gsl_ran_ugaussian(r));

	gsl_blas_dtrmv(CblasLower, CblasNoTrans, CblasNonUnit, work, result);
	gsl_vector_add(result, mean);

	gsl_matrix_free(work);

	return 0;
}

//the posterior PDF
double target(double x[2]) {
	return exp(-pow(x[0], 2) - pow(x[1] * x[0], 2) - pow(x[1], 2));
}


#endif /*METROPHASTING_H*/ 



"GibbsM.h":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef GIBBSM_H
#define GIBBSM_H
#include <iostream>
#include <cstdlib>
#include "chartdir.h"
#include <string>
#include <sstream>
#include <array>
#include <gsl/gsl_sf_gamma.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
   #include <gsl/gsl_blas.h>
   #include <gsl/gsl_linalg.h>

  namespace GBS {
	
     void gibbsSamplerArr(const int sampleSize, double correlation, const char* description, const char* xAxis, const char* yAxis,
		const char* jpegName) {
		double * arrX = NULL;
		double * arrY = NULL;
		arrX = (double*)calloc(sampleSize, sizeof(double));
		arrY = (double*)calloc(sampleSize, sizeof(double));
		if (arrX == NULL) {
      std::cout << "Memory allocation for gibbsSamplerArr failed." << std::endl;
		}
      if (arrY == NULL) {
      std::cout << "Memory allocation for gibbsSamplerArr failed." << std::endl;
		}
		int n = sampleSize, i;
		double x, y, rho = correlation, sd;
		gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
		sd = sqrt(1 - rho * rho);
		x = 0; y = 0;
		std::cout << x << " " << y << std::endl;
		for (i = 1; i < n; i++) {
			x = rho * y + gsl_ran_gaussian(r, sd);
			y = rho * x + gsl_ran_gaussian(r, sd);
			arrX[i] = x;
			arrY[i] = y;
			std::cout << x << " " << y << std::endl;
		}
             // Create a XYChart object of size 450 x 420 pixels
		XYChart *gibbsPlot = new XYChart(450, 420);
        // Set the plotarea at (55, 65) and of size 350 x 300 pixels, with a light grey border
      // (0xc0c0c0). Turn on both horizontal and vertical grid lines with light grey color (0xc0c0c0)
   gibbsPlot->setPlotArea(55, 65, 350, 300, -1, -1, 0xc0c0c0, 0xc0c0c0, -1);

  // Add a legend box at (50, 30) (top of the chart) with horizontal layout. Use 12pt Times Bold
	// Italic font. Set the background and border color to Transparent.
        gibbsPlot->addLegend(50, 30, false, "timesbi.ttf"-12)->setBackground(Chart::Transparent);
           // Add a title to the chart using 18pt Times Bold Itatic font.
		gibbsPlot->addTitle(description, "timesbi.ttf", 18);
             // Add a title to the y axis using 12pt Arial Bold Italic font
		gibbsPlot->yAxis()->setTitle(xAxis, "arialbi.ttf", 12);
            // Add a title to the x axis using 12pt Arial Bold Italic font
		gibbsPlot->xAxis()->setTitle(yAxis, "arialbi.ttf", 12);
               // Set the axes line width to 3 pixels
		gibbsPlot->xAxis()->setWidth(3);
		gibbsPlot->yAxis()->setWidth(3);
		//1st CHART OF SPHERICAL RHO POSITION AGAINST DIRECTIONS      
        // Add an orange (0xff9933) scatter chart layer, using 13 pixel diamonds as symbols
		gibbsPlot->addScatterLayer(DoubleArray(arrX, (int)(sizeof(arrX) / sizeof(arrX[0]))), DoubleArray(
	arrY, (int)(sizeof(arrY) / sizeof(arrY[0]))), "Red(y dependent on x)",
	Chart::CircleShape, 13, 0xff0000);//RED
      gibbsPlot->addScatterLayer(DoubleArray(arrY, (int)(sizeof(arrY) / sizeof(arrY[0]))), DoubleArray(
	arrX, (int)(sizeof(arrX) / sizeof(arrX[0]))), "Blue (x dependent on Y)",
        Chart::CircleShape, 13, 0x0000ff);//BLUE
		// Output the chart
		gibbsPlot->makeChart(jpegName);
          //free up resources
	  delete gibbsPlot;
            }
     }//namespace
  #endif /*GIBBSM_H*/ 

Could someone please tell me how to go about fixing these errors as I feel they are no native to the code I typed.
Try adding legacy_stdio_definitions.lib to the linker libs.
@tpb- i just added legacy_stdio_definitions.lib into the additional dependencies sction of the Linker in the Input section and now I the errors have reduced to:

gsl.lib(error.obj) : error LNK2019: unresolved external symbol ___iob_func referenced in function _gsl_error
1>gsl.lib(stream.obj) : error LNK2001: unresolved external symbol ___iob_func
1>cblas.lib(xerbla.obj) : error LNK2001: unresolved external symbol ___iob_func
1>c:\users\pmbanugo\documents\visual studio 2015\Projects\MonteCarloDemo\Debug\MonteCarloDemo.exe : fatal error LNK1120: 1 unresolved externals

Any more ideas to get rid of the remaining errors, thanks for the help.
Last edited on
Topic archived. No new replies allowed.