I have a C project in Visual Studio 2019 and I would like to use certain functions written in Go within it.
I have a test file called ctest.go which has the following contents:
package main
import (
"C"
"fmt"
)
//export printInt
func printInt(x int) {
fmt.Printf("Hello from go: %d", x)
}
func main() {}
I'm generating a static library with this command:
go build -buildmode=c-archive ctest.go
I end up with two files, ctest.a and ctest.h which I include in my VS project. I include ctest.h in my code and immediately get these errors:
identifier "__SIZE_TYPE__" is undefined
expected a ';'
expected a ';'
The generated header file contains the following lines which caused it:
typedef __SIZE_TYPE__ GoUintptr;
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
I replaced __SIZE_TYPE__ by size_t and just commented out the _Complex lines to stop the errors.
Now my VS project compiles while including the header file. But as soon as I try to use my go function like this:
printInt(5);
I get 2 warnings followed by 6 errors:
Warning LNK4078 multiple '.text' sections found with different attributes (60600060) AWO F:\AWO\AWO\ctest.a(go.o)
Warning LNK4078 multiple '.text' sections found with different attributes (60600060) AWO F:\AWO\AWO\ctest.a(go.o)
Error LNK2019 unresolved external symbol __imp___iob referenced in function __cgo_preinit_init AWO F:\AWO\AWO\ctest.a(000005.o)
Error LNK2001 unresolved external symbol __imp___iob AWO F:\AWO\AWO\ctest.a(000006.o)
Error LNK2001 unresolved external symbol __imp___iob AWO F:\AWO\AWO\ctest.a(000007.o)
Error LNK2019 unresolved external symbol _fprintf referenced in function _x_cgo_sys_thread_create AWO F:\AWO\AWO\ctest.a(000005.o)
Error LNK2001 unresolved external symbol _fprintf AWO F:\AWO\AWO\ctest.a(000007.o)
Error LNK1120 2 unresolved externals AWO F:\AWO\Debug\AWO.exe 1
This is where I'm stuck. I'm not sure what I'm doing wrong or should be doing differently to properly use the library generated with go.
project properties -> linker -> input -> additional dependencies
legacy_stdio_definitions.lib;%(AdditionalDependencies)
add source code
C
FILE* __cdecl __iob_func(void)
{
FILE _iob[] = { *stdin, *stdout, *stderr };
return _iob;
}
C++
extern "C" { FILE __iob_func[3] = { *stdin,*stdout,*stderr }; }
Related
I get the following linker error:
1>------ Build started: Project: Console1, Configuration: Debug Win32 ------
1>Compiling with Intel(R) Visual Fortran Compiler 18.0.2.185 [IA-32]...
1>Console1.f90
1>Linking...
1>Console1.obj : error LNK2019: unresolved external symbol __imp__SETFLUIDSDLL referenced in function _MAIN__
1>Debug\Console1.exe : fatal error LNK1120: 1 unresolved externals
1>
The source is
program Console1
implicit none
INTERFACE
SUBROUTINE SETFLUIDSdll(hfld,ierr, hFld_length)
!DEC$ ATTRIBUTES DLLIMPORT :: SETFLUIDSdll
!DEC$ ATTRIBUTES REFERENCE::hfld, ierr, hFld_length
CHARaCTER(*) hfld
INTEGER ierr, hFld_length
END SUBROUTINE SETFLUIDSdll
END INTERFACE
! Variables
CHARACTER (len = 40) :: hfld
INTEGER ierr
INTEGER hFld_length
hfld = 'Test'
! Body of Console1
print *, 'Hello World'
call SETFLUIDSdll(hfld, ierr, hFld_length)
end program Console1
I have a lib added containing the SETFLUIDSdll method. I do not know why the linker ist requiring a _imp__SETFLUIDSDLL. From where is the imp coming?
You also asked this in the Intel support forum. The __imp__ is generated by the compiler when you use a DLLIMPORT directive. When the DLL is linked, the export library, created by the linker, generates names with the same prefix. The purpose of this is to create a more efficient connection between the call and the DLL, saving a few instructions.
The reason you're getting the link error is that you have not linked to the export library (.LIB) generated when the DLL was linked. (Either that, or you failed to add DLLEXPORT directives for the procedure in the DLL.) The fix is to make sure that the DLL's export library is linked in with the executable.
I'm trying to create an object of IVpnManagementAgent but I can't seem to find where the IID (IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent) of it is defined so I can instance RoGetActivationFactory as it seems it requires me a second parameter of REFIID.
Also I saw this example but as I'm using C - I can't use __uuidof.
Weirdly enough the msdn sample seems to omit this parameter.
The problem is if I compile this code:
#include <roapi.h>
#define COBJMACROS
#include <windows.networking.vpn.h>
(main)()
{__x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent *pVpnMan;
IClassFactory *pCF; RoInitialize(RO_INIT_MULTITHREADED),
RoGetActivationFactory(0,
&IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent,
&pVpnMan);}
Like this: cl rmousevpn.c /link RuntimeObject.lib windows.networking.lib
It gives me unresolved:
rmousevpn.obj : error LNK2019: unresolved external symbol IID___x_ABI_CWindows_CNetworking_CVpn_CIVpnManagementAgent referenced in function main
rmousevpn.exe : fatal error LNK1120: 1 unresolved externals
I read section Compilation on Windows in installation page but I still very confused, I hope some experts can enlighten me.
I downloaded LATEST.tar.gz from here.
After that, I copied sodium.h and sodium folder in libsodium-1.0.12\src\libsodium\include to my project.
Here is the code:
#include <stdio.h>
#include "sodium.h"
#pragma warning (disable:4996)
void main()
{
char myString[32];
uint32_t myInt;
/* myString will be an array of 32 random bytes, not null-terminated */
randombytes_buf(myString, 32);
/* myInt will be a random number between 0 and 9 */
myInt = randombytes_uniform(10);
printf("%d", myInt);
system("pause");
}
And I get these errors when compiling:
Error LNK1120 2 unresolved externals
Error LNK2019 unresolved external symbol __imp__randombytes_buf
referenced in function _main
Error LNK2019 unresolved external symbol __imp__randombytes_uniform
referenced in function _main
I did not get error like "cannot open sodium.h".
How can I solve this?
Any help is appreciated.
Your errors are telling you there's a problem at link time - so your issue isn't with including sodium.h. There's a library that's not being added to your project. You can't just copy the library to your project directory, you need to tell Visual Studio to link it in.
I'm trying to include Csparse from SuiteSparse in to Simulink.
I've included the header ch.h and the directory of the c-functions in the custom code section.
If I call the function cs_compress over code.ceval I get this error:
cs_compress.obj : error LNK2019: unresolved external symbol cs_spalloc referenced in function cs_compress
Test2_sfun.mexw64 : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\amd64\link.exe"' : return code '0x460'
Stop.
The function cs_spalloc I presume is called from a library since there exists no cs_spalloc.c. The function is however defined in the header file as:
cs *cs_spalloc (csi m, csi n, csi nzmax, csi values, csi triplet) ;
Am I right to presume it is called from a library?
And if so where do I find the library in a format to include in to the custom code section in Simulink?
I found the c- functions cs_spalloc online, and just copyed it. Now it works.
I am new to learning openCV and was trying to reproduce an online example "face recognition" program here: http://www.cognotics.com/opencv/servo_2007_series/part_5/index.html.
I am however getting an "unresolved external symbol" error for few functions:
cvEigenDecomposite
cvCalcEigenObjects
indNearestNeighbor(float *) // functions defined locally by the user
loadTrainingData(struct CvMat *) // functions defined locally by the user
The "unresolved external symbol" would show up when I mess up the linker. The weird thing, it worked just fine for other examples I have done so far on other websites. My reasoning is that the first two functions cvEigenDecomposite and cvCalcEigenObjects require extra .lib object that I haven't linked to and the other two functions depend on the first two. So my question is whether those functions require extra .lib objects to be linked to?
here is the list of the .lib objects I currently link to:
opencv_core231d.lib
opencv_highgui231d.lib
opencv_imgproc231d.lib
opencv_features2d231d.lib
opencv_calib3d231d.lib
Thanks!
EDIT:
ERRORS:
1>main.obj : error LNK2019: unresolved external symbol _cvEigenDecomposite referenced in function "void __cdecl learn(void)" (?learn##YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol _cvCalcEigenObjects referenced in function "void __cdecl doPCA(void)" (?doPCA##YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl findNearestNeighbor(float *)" (?findNearestNeighbor##YAHPAM#Z) referenced in function "void __cdecl recognize(void)" (?recognize##YAXXZ)
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl loadTrainingData(struct CvMat * *)" (?loadTrainingData##YAHPAPAUCvMat###Z) referenced in function "void __cdecl recognize(void)" (?recognize##YAXXZ)
1>C:\Users\Lyukshins\Dropbox\Programming\OpenCV2\Face Recognition\Debug\Face Recognition.exe : fatal error LNK1120: 4 unresolved externals
DEPENDENCIES:
C:\opencv\build\include
C:\opencv\build\include\opencv2
C:\opencv\build\include\opencv
INCLUDED DIRECTORIES:
C:\opencv\build\x86\vc10\lib
C:\opencv\build\include
cvEigenDecomposite and cvCalcEigenObjects are located in opencv_legacy231d.lib
The other 2 functions, as you said, are local functions. So they must be declared in the code before the functions that call them.