Mingw: Undefined reference linking with boost program_options - linker

I am using ming64 (gcc 4.5.4) and my test program looks like this:
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <boost/program_options.hpp>
#include <boost/program_options/detail/config_file.hpp>
int main()
{
std::ifstream ifs("");
std::map<std::string, std::string> parameters;
std::set<std::string> options;
for (boost::program_options::detail::config_file_iterator i(ifs, options, true), e; i != e; ++i)
{
parameters[i->string_key] = i->value[0];
}
return 0;
}
When I compile it I get:
Linking CXX executable main.exe
CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `basic_config_file_iterator':
C:/Users/test/Desktop/po/src/include/boost/program_options/detail/config_file.hpp:147: undefined reference to `boost::program_options::detail::common_config_file_iterator::common_config_file_iterator(std::set<std::string, std::less<std::string>, std::allocator<std::string> > const&, bool)'
C:/Users/test/Desktop/po/src/include/boost/program_options/detail/config_file.hpp:150: undefined reference to `boost::program_options::detail::common_config_file_iterator::get()'
CMakeFiles\main.dir/objects.a(main.cpp.obj): In function `boost::eof_iterator<boost::program_options::detail::common_config_file_iterator, boost::program_options::basic_option<char> >::increment()':
C:/Users/test/Desktop/po/src/include/boost/program_options/eof_iterator.hpp:75: undefined reference to `boost::program_options::detail::common_config_file_iterator::get()'
collect2: ld returned 1 exit status
mingw32-make[2]: *** [main.exe] Error 1
mingw32-make[1]: *** [CMakeFiles/main.dir/all] Error 2
mingw32-make: *** [all] Error 2
I link with -Lpath_to_lib and -llibboost_program_options-mgw45-mt-1_48. And there are two files:
libboost_program_options-mgw45-mt-1_48.dll libboost_program_options-mgw45-mt-1_48.dll.a
nm gives the following output for the library:
$ nm libboost_program_options-mgw45-mt-1_48.dll|grep common_config_file_iterator
000000006395e0a0 d .data$_ZTIN5boost12eof_iteratorINS_15program_options6detail27common_config_file_iteratorENS1_12basic_optionIcEEEE
000000006395e0c0 d .data$_ZTIN5boost15iterator_facadeINS_15program_options6detail27common_config_file_iteratorEKNS1_12basic_optionIcEENS_21forward_traversal_tag
ERS6_xEE
000000006395e440 d .data$_ZTIN5boost15program_options6detail27common_config_file_iteratorE
000000006395f6a0 d .data$_ZTVN5boost15program_options6detail27common_config_file_iteratorE
0000000063962d60 r .rdata$_ZTSN5boost12eof_iteratorINS_15program_options6detail27common_config_file_iteratorENS1_12basic_optionIcEEEE
0000000063962de0 r .rdata$_ZTSN5boost15iterator_facadeINS_15program_options6detail27common_config_file_iteratorEKNS1_12basic_optionIcEENS_21forward_traversal_ta
gERS6_xEE
0000000063963560 r .rdata$_ZTSN5boost15program_options6detail27common_config_file_iteratorE
0000000063933ce0 t .text$_ZN5boost15program_options6detail27common_config_file_iterator7getlineERSs
0000000063933cf0 t .text$_ZN5boost15program_options6detail27common_config_file_iteratorD0Ev
0000000063933ec0 t .text$_ZN5boost15program_options6detail27common_config_file_iteratorD1Ev
0000000063934080 t .text$_ZN5boost15program_options6detail27common_config_file_iteratorD2Ev
00000000639100a0 t _GLOBAL__I__ZN5boost15program_options6detail27common_config_file_iteratorC2ERKSt3setISsSt4lessISsESaISsEEb
000000006390da90 T _ZN5boost15program_options6detail27common_config_file_iterator10add_optionEPKc
000000006390e6d0 T _ZN5boost15program_options6detail27common_config_file_iterator3getEv
U _ZN5boost15program_options6detail27common_config_file_iterator7getlineERSs
0000000063933ce0 T _ZN5boost15program_options6detail27common_config_file_iterator7getlineERSs
000000006390e3f0 T _ZN5boost15program_options6detail27common_config_file_iteratorC1ERKSt3setISsSt4lessISsESaISsEEb
000000006390e3f0 T _ZN5boost15program_options6detail27common_config_file_iteratorC2ERKSt3setISsSt4lessISsESaISsEEb
U _ZN5boost15program_options6detail27common_config_file_iteratorD0Ev
0000000063933cf0 T _ZN5boost15program_options6detail27common_config_file_iteratorD0Ev
U _ZN5boost15program_options6detail27common_config_file_iteratorD1Ev
0000000063933ec0 T _ZN5boost15program_options6detail27common_config_file_iteratorD1Ev
0000000063934080 T _ZN5boost15program_options6detail27common_config_file_iteratorD2Ev
000000006390d900 T _ZNK5boost15program_options6detail27common_config_file_iterator14allowed_optionERKSs
000000006395e0a0 D _ZTIN5boost12eof_iteratorINS_15program_options6detail27common_config_file_iteratorENS1_12basic_optionIcEEEE
000000006395e0c0 D _ZTIN5boost15iterator_facadeINS_15program_options6detail27common_config_file_iteratorEKNS1_12basic_optionIcEENS_21forward_traversal_tagERS6_x
EE
000000006395e440 D _ZTIN5boost15program_options6detail27common_config_file_iteratorE
0000000063962d60 R _ZTSN5boost12eof_iteratorINS_15program_options6detail27common_config_file_iteratorENS1_12basic_optionIcEEEE
0000000063962de0 R _ZTSN5boost15iterator_facadeINS_15program_options6detail27common_config_file_iteratorEKNS1_12basic_optionIcEENS_21forward_traversal_tagERS6_x
EE
0000000063963560 R _ZTSN5boost15program_options6detail27common_config_file_iteratorE
000000006395f6a0 D _ZTVN5boost15program_options6detail27common_config_file_iteratorE
What is the cause of the undefined reference (same code works on linux just fine)?

Related

GSL: how to get LDLT decomposition in GSL

I want to get the matrix l and d of matrix Q from LDLT decomposition. The same result of scipy.linalg.ldl(),here is the code:
#include <gsl/gsl_math.h> #include <gsl/gsl_sf.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 <gsl/gsl_cdf.h>
#include <gsl/gsl_errno.h>
int main() {
const int dim = 3;
double pars[3] = { 0, 0, 0 };
double Q[9] = {
2,-1,0,
-1,3,-1,
0,-1,4
};
int i=0,j=0;
// Gaussian Multivariate distribution
gsl_matrix *L = gsl_matrix_calloc(dim, dim);
gsl_vector *S = gsl_vector_calloc(dim);
gsl_permutation * perm = gsl_permutation_calloc(dim);
for(i=0;i<dim*dim;i++) L->data[i]=Q[i];
>> gsl_linalg_ldlt_decomp(L);
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.4f ", L->data[i*3+j]);
}
printf("\n");
}
printf("\n S=");
for(i=0;i<3;i++)
printf("%.4f ", S->data[i]);
printf("\n");
}
my compile args is gcc ldl.c -lm -llapack -lblas -lgsl
But it returns;
ldl.c: In function ‘main’:
ldl.c:39:5: warning: implicit declaration of function ‘gsl_linalg_ldlt_decomp’; did you mean ‘gsl_linalg_PTLQ_decomp’? [-Wimplicit-function-declaration]
39 | gsl_linalg_ldlt_decomp(L);
| ^~~~~~~~~~~~~~~~~~~~~~
| gsl_linalg_PTLQ_decomp
/usr/bin/ld: /tmp/ccSWXMMb.o: in function `main':
ldl.c:(.text+0x170): undefined reference to `gsl_linalg_ldlt_decomp'
collect2: error: ld returned 1 exit status
WHy ? What shoud I do?
Perhaps the best way of compiling GSL programs via the command-line is by using the flags provided by GSL itself. They can be read via the
gsl-config
utility.
In your case use it as follows (providing your shell can properly process the backquoteses):
gcc ldl.c `gsl-config --libs`
If not, use the output of gsl-config --libs directly as the command line arguments.
Anyway, your code compiles and runs on my system without any problem.

How to fix undefined reference to pow in C Lion in Linux?

I have already taken a look at the question :Undefined reference to `pow' and `floor' but I'd like to know if there is anyway I could fix this permanently in my IDE which is Jet brains C Lion. I am using it on Pop OS. And I am very new to C as well as programming in general, it'd be better if I could just build and compile the program from my IDE rather than having to go to terminal every time.
Edit #1 : This is the code I am trying to compile rn. Its trying to find the reverse of a number rn.
#include <stdio.h>
#include <math.h>
int how_long(int x)
{
int count = 0;
while (x>0)
{
x /= 10;
count++;
}
return count;
}
int main()
{
int n = 1;
int num ;
int reverse_num = 0;
printf("Enter a number : ");
scanf("%i",&num);
int j = how_long(num);
while (num>0)
{
reverse_num += (num % 10) * (int)pow(10,j) ;
num -= (num%10) * (int)pow(10,n-1);
++n;
--j;
}
printf("%i",reverse_num);
return 0;
}
And the error I get on C Lion is :
[1/1] Linking C executable 5_2
FAILED: 5_2
: && /usr/bin/cc -g CMakeFiles/5_2.dir/main.c.o -o 5_2 && :
/usr/bin/ld: CMakeFiles/5_2.dir/main.c.o: in function `main':
/home/koustubhjain/CLionProjects/Assignments/5-2/main.c:30: undefined reference to `pow'
/usr/bin/ld: /home/koustubhjain/CLionProjects/Assignments/5-2/main.c:31: undefined reference to `pow'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
Edit #2 :
I found this old forum post on C Lion's website : https://intellij-support.jetbrains.com/hc/en-us/community/posts/206607085-CLion-Enabling-math-h-for-C-projects
but idk if it still works, and if it works, how do I add that to my CMake file ?
Here is my CMakeLists.txt file for reference :
cmake_minimum_required(VERSION 3.21)
project(5_2 C)
set(CMAKE_C_STANDARD 99)
add_executable(5_2 main.c)
Edit #3 : Edit #2 seems to have fixed it

Why "undefined symbols" on gcc when main includes header file? [duplicate]

This question already has answers here:
undefined symbol for architecture x86_64 in compiling C program [duplicate]
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 3 years ago.
Why does main.c fail to compile when including sum_divisibles.h?
I am sure I am missing something silly, but can't find it for the life of me.
Compiling on Mac OSX.
sum_divisibles.h
#ifndef SUM_DIVISIBLES
#define SUM_DIVISIBLES
int sum_divisibles(int limit);
#endif /* SUM_DIVISIBLES */
sum_divisibles.c
#include <stdlib.h>
#include "sum_divisibles.h"
int sum_divisibles(int limit)
/* Returns sum of multiples of 3 and 5 less than limit. */
{
int mult_3, mult_5, sum = 0;
for (int ndx=0; (mult_3 = ndx * 3 ) < limit; ndx++) {
if ( (mult_5 = ndx * 5) < limit && mult_5 % 3 != 0 ) {
sum += mult_5 ;
}
}
return sum; }
main.c
#include <stdio.h>
#include "sum_divisibles.h" // does not compile
//#include "sum_divisibles.c" // compiles
int main(void)
{
printf("%d\n", sum_divisibles(1000));
}
Error -- on gcc -Wall main.c
Undefined symbols for architecture x86_64:
"_sum_divisibles", referenced from:
_main in main-e4e97
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

C : Including Header file with .dll file and .lib file on Linux

I have a three files, Oracle.h, Oracle.dll, Oracle.lib.
These files are in same directory of main.c file.
And I want to include Oracle.h file.
#include <stdio.h>
#include "Oracle.h"
#pragma comment(lib, "Oracle.lib")
int main(void)
{
int crit;
char P[36] = { 0, };
char C[36] = { 0, };
int Clen = 36;
crit = Oracle(C, Clen);
printf("%d", crit);
return 0;
}
I used this code for main.c file, and compile by make main command.
But compiling this file has error, and I don't know what should I do.
$ make problem4_oracle
cc problem4_oracle.c -o problem4_oracle
/var/tmp/cc9oUwU3.o: In function main':
problem4_oracle.c:(.text+0x79): undefined reference to Oracle'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'problem4_oracle' failed
make: *** [problem4_oracle] Error 1
Please help me.

How to compile an application using libcurl with gcc on mingw

I am trying to compile a C file containing this code :
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
int main()
{
curl_global_init( CURL_GLOBAL_ALL );
CURL * myHandle;
CURLcode result; // We’ll store the result of CURL’s webpage retrieval, for simple error checking.
myHandle = curl_easy_init ( ) ;
// Notice the lack of major error checking, for brevity
curl_easy_setopt(myHandle, CURLOPT_URL, "http://www.example.com");
result = curl_easy_perform( myHandle );
curl_easy_cleanup( myHandle );
printf("LibCurl rules!\n");
return 0;
}
,
Well when I try to compile :
gcc url.c -lcurl
I get the following errors:
F:\MinGW\home>gcc url.c -lcurl
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0x8e): undefined reference to `_imp__curl_global_init'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0x95): undefined reference to `_imp__curl_easy_init'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xbf): undefined reference to `_imp__curl_easy_setopt'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xcd): undefined reference to `_imp__curl_easy_perform'
C:\Users\Ehsan\AppData\Local\Temp\ccF7doFN.o:url.c:(.text+0xdf): undefined reference to `_imp__curl_easy_cleanup'
collect2.exe: error: ld returned 1 exit status
I downloaded this distribution
and copied all files to bin, include , and lib folder.
What am I missing?
Update
Well I fixed the problem by using -lcurldll.
In my lib folder I have both libcurl.a and libcurldll.a.
Why linking with libcurl.a can't compile but with libcurldll.a it works fine?
You can check the lib name by:
readelf -d [LIB_PATH]
That would solve you the problem.

Resources