c++ - g++ - Finding appopriate Windows libraries to link so as to compile FANN library -


for various reasons have been trying compile fann library myself. i'm on windows 10 mingw. keep things simple going start with:

g++ mushroom.c -o shroom.exe -lm -i src\ -i src\include\ src\doublefann.c 

(mushroom.c includes <stdio.h> , "fann.h".)

using -i src\ -i src\include\ src\doublefann.c allowed me rid of various undefined reference errors resulting header files not being found, keeps throwing following undefined reference:

doublefann.c:(.text+0x4ee9): undefined reference gettickcount() 

fyi, appears in fann.h (line 54):

/* compat_time replacement */  #ifndef _win32 #include <sys/time.h> #else   /* _win32 */ #if !defined(_msc_extensions) && !defined(_inc_windows)   extern unsigned long __stdcall gettickcount(void); 

in short, seems error linking windows libraries, , don't know how proceed find relevant ones link.

here full fann.h , full doublefann.c

disclaimers , notes

edit: since going bed last night, refined approach don't have edit actual fann source files. (for record, original approach worked , can @ edit history see if care).

firstly, else stumbling across question, should note it's not necessary use fann way. website provides cmake files visual studio solutions both work fine.

 

whats wrong

looking @ question, problem obvious , hate myself not seeing before first of way many hours. you'll notice of files in command have *.c extension. forgive me treating novice, can't assume know or risk giving bad answer. *.c canonical file extension c source files. gnu toolchain flexible piece of software, , can compile c code g++ without errors of time. shouldn't, because you'll error. since source file, mushroom.c, c source file, it'll work compile code c (in case gcc, not g++). if, however, you're using things in program iostream or string or classes or other c++ code, you're right compile c++ (by convention change extension *.cpp clarification, i'm not sure gnu toolchain cares). in case, whenever want include c code in c++ program, sure wrap in extern "c"{}, compiler knows calling conventions , labels c standard, isn't same c++ standard. means not calling c code c++ without extern "c" risk passing arguments functions in wrong way (good way cause segfault), way variables , functions renamed when compiled assembly different between 2 languages, why undefined reference in linking stage, not compiling stage. in case, not doublefann.c c source file (which links fine), library contains function declaration of gettickcount(). specifically, you're linking %systemroot%\system32\kernel32.dll (normally, believe mingw uses path_to_mingw\lib\lib32k.a), defines how programs can interact windows operating system @ machine-code level. __stdcall utility defined here call things gettickcount() os, assembly functions, within c code. because in *.dll, compiler cannot change __stdcall suit c++ coventions. due fact "dll" stands "dynamically linked library". means these files compiled machine code when windows (or mingw) installed. editing nonsensical, because you'd need have intimate working knowledge of opcodes every x86_64 processor ever made. thus, calling in c in way __stdcall expects, calling in c++ requires extern "c", or it'll mangle names of function declaration , possibly cause runtime errors.

tl;dr

fann written in c, c=/=c++, don't expect c++ compiler compile c code perfectly.

 

solution

there 2 ways go solving problem.

1

if you're using c++ features/libraries, change name of source code mushroom.cpp (if want) , change line (wherever occurs in program)

#include "doublefann.c" 

to wrapped so:

extern "c"{     #include "doublefann.c" } 

if read through fann.h, might have noticed lines:

#ifdef __cplusplus //line 65 extern "c" {  #ifndef __cplusplus } /* fool automatic indention engines */  #endif #endif  /* __cplusplus */ 

don't worry, these don't appear conflict. frankly, i'm not sure these lines meant accomplish, i'm sure know they're doing.

2

if mushroom.c pure c, compile using:

gcc -o shroom.exe mushroom.c -lm -i src\ -i src\ -i src\include\ src\doublefann.c -wall 

and should work. added -wall because love being able make code absolutely perfect, feel free leave out prints every warning have.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -