Visual Fortran Linker error obsolete _imp - linker

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.

Related

Access Golang code from a C project in Visual Studio 2019

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

Linking a C module to MASM in Visual Studio

I am working on a big MASM project (I've Compiled my MASM code using the ML compiler under VS 2019) , and I would like to call a C function I wrote, within my MASM code. I googled it a lot , but I couldn't find anything useful. How can I achieve that?
When I just tried to add my C source-code to the project and rebuild it , I got a bunch of linking errors (LNK2019) , like these:
LNK2019 unresolved external symbol __CrtDbgReport referenced in function __CRT_RTC_INIT
LNK2019 unresolved external symbol __CrtDbgReportW referenced in function __CRT_RTC_INITW
LNK2019 unresolved external symbol ___stdio_common_vsprintf_s referenced in function __vsprintf_s_l
Here is a screenshot of the errors window
Answering my question
I've finally figured out a way to do this.
Create a Static library (.lib) from your C/C++ module.
Properies -> Project Defaults -> Configuration Type -> change to .lib
and then, In your masm code , add these lines:
includelib your_generated_lib.lib
(Be sure to add your lib file to the directory of your masm code)
and add prototypes to your functoins like this:
function_name PROTO C :DWORD ;(Just an Example )
Hope it helps,
Omer

Function not found when compiling a Fortran program against a static library (in Windows using IFORT)

Given the following Fortran module:
MODULE Test
IMPLICIT NONE
INTERFACE
INTEGER(c_int) FUNCTION process_(script, script_size) BIND(C, name = "process")
USE, INTRINSIC :: iso_c_binding, ONLY: c_int, c_char
CHARACTER(c_char), INTENT(IN) :: script(*)
INTEGER(c_int), INTENT(IN), VALUE :: script_size
END FUNCTION
END INTERFACE
CONTAINS
FUNCTION process(script)
#ifdef SHARED_LIB
!DEC$ ATTRIBUTES DLLEXPORT :: process
#endif
INTEGER :: process
CHARACTER(LEN = *), INTENT(IN) :: script
process = process_(script, LEN(script))
END FUNCTION
END MODULE
I am able to successfully compile this module using IFORT (in Windows), both as a shared library and a static library.
Additionally, I am able to successfully compile a small Fortran program (named example.f90) that uses the shared library. The small program is the following:
PROGRAM Example
USE Test
INTEGER :: state
state = process("Hello world!")
END PROGRAM
Now, when I try to compile the same small Fortran program this time using the static library it gives the following error:
C:\workspace\>ifort.exe /Qopenmp /module:library library\Test.lib example.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 16.0 Build 20160811
Copyright (C) 1985-2016 Intel Corporation. All rights reserved.
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.
-out:example.exe
-subsystem:console
-defaultlib:libiomp5md.lib
-nodefaultlib:vcomp.lib
-nodefaultlib:vcompd.lib
library\Test.lib
example.obj
example.obj : error LNK2019: unresolved external symbol __imp_TEST_mp_process referenced in function MAIN__
example.exe : fatal error LNK1120: 1 unresolved externals
Any idea how to solve this?
The DLLEXPORT in routine process is turned into a DLLIMPORT when you USE the module (this is a feature added some versions back, but I don't remember exactly when.) The compiler is therefore assuming that process comes from a DLL and adds the __imp_ prefix, but since you built the library as a static library it is not found. You can 1) Remove the DLLEXPORT, 2) use conditional compilation based on the _DLL symbol (predefined for DLL builds) to enable DLLEXPORT only then, 3) build the library as a DLL.
I will also caution you that order of libraries and objects in the link step can matter, though I don't think it does here. I would recommend putting libraries after sources when building the executable.

Including c libraries in the custome code section of Simulink

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.

Unresolved symbols when linking a program using libcurl

I know this is programming questions but I'm just frustrated trying to figure out what I'm doing wrong..
I'm using visual studio 2010 and followed all the steps here: http://curl.haxx.se/libcurl/c/visual_studio.pdf
When I try to compile my solution I keep getting this error:
1>------ Build started: Project: LibCurl, Configuration: Debug Win32 ------
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_cleanup referenced in function _main
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_perform referenced in function _main
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_setopt referenced in function _main
1>LibCurl.obj : error LNK2019: unresolved external symbol __imp__curl_easy_init referenced in function _main
1>C:\Users\Kyle\Documents\Visual Studio 2010\libcurl\VisualStudio\LibCurl\Debug\LibCurl.exe : fatal error LNK1120: 4 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Source:
// LibCurl.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, "http://google.com");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
return 0;
}
I've been using static version of libcurl, and to link my program against it properly, I had to add definition:
CURL_STATICLIB
to build configuration of my project.
Besides defining CURL_STATICLIB, for me it was also necessary to link the following dependencies (including libcurl.lib or libcurld.lib):
Ws2_32.lib
Wldap32.lib
I ran into a similar issue - found that I was referencing the 64-bit location of libcurl.lib. Changed the link directory to the 32-bit location and the project compiled perfectly.
Looks like the libraries are not being successfully linked. Ensure the library directory is set to include the full path to the libcurl dll. Also make sure this library is actually added to your project.
I had the same problem. I wrote how I finally was able to make CurlLib works, here:
http://quantcorner.wordpress.com/2012/04/08/using-libcurl-with-visual-c-2010/ if you wish to have a look. Good luck!
This worked for me on VS2017 - x86 Release/Debug - MFC Static Library
Open project properties and review the following
C/C++ - Preprocessor - Preprocessor Definitions - Add CURL_STATICLIB
Linker - Input - Additional Dependencies - Add (CTRL+C)
ws2_32.lib
Normaliz.lib
Crypt32.lib
Wldap32.lib
libcurl_a.lib (libcurl_a_debug.lib for debug configuration)
C/C++ - General - Additional Include Directories - Add include
folder to header files
After many ideas and configurations, I solved the problem adding this:
#pragma comment(lib, "lib/libcurl_a.lib")
where libcurl_a.lib is the name of the curl lib file and lib is the folder which contains it.
I had the same error, the problem I had was that I built cURL according to this SO answer, which doesn't work if you wish /MT as a runtime library option.
In order to built cURL with respect to /MT and /MTD you have to also execute Set RTLIBCFG=static before actually building it with the nmake command in the very same console. Full process of building cURL this way can be found here.
Edit:
In case the URL stops working, I will also put the instructions here:
Download and extract the CUrl source code to a temp directory. http://curl.haxx.se/download.html, in this tutorial we will be using curl-7.37.0
Open the “Visual Studio Command Prompt (2010)”
Browse to the Winbuilds folder. \curl-7.37.0\winbuild.
Type Set RTLIBCFG=static into the command prompt and hit enter. This will set up the compiler to build for /MT and /MTd.
Type nmake /f MakeFile.vc mode=static DEBUG=yes to build the debug version or
Type nmake /f MakeFile.vc mode=static DEBUG=no to build the release versions.

Resources