clang++ (version 5) and LNK4217 warning - linker

I am just learning how to code.
I have installed clang version 5 on a windows 10 system using visual studio 14.
I created a hello world cpp file to test that is working.
Sample code
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!\n";
int rip{1};
int dal{4};
int kane = rip + dal;
cout << kane;
return 0;
}
command
clang++ -o .\bin\testing.exe test.cpp
Clang does compile and I get an executable which does run as expected. however I do receive this message.
test-3e53b3.o : warning LNK4217: locally defined symbol ___std_terminate imported in function "int `public: __thiscall std::basic_ostream<char,struct std::char_traits<char> >::sentry::~sentry(void)'::`1'::dtor$5" (?dtor$5#?0???1sentry#?$basic_ostream#DU?$char_traits#D#std###std##QAE#XZ#4HA)
test-3e53b3.o : warning LNK4217: locally defined symbol __CxxThrowException#8 imported in function "public: void __thiscall std::ios_base::clear(int,bool)" (?clear#ios_base#std##QAEXH_N#Z)
I have searched online and can find similar issues but they are not the same.
I realise this maybe simple to you guys, but I am at a loss I have used various IDES and GCC and this code has not produced this warning before.

Add -Xclang -flto-visibility-public-std to your compiler options.
Like so:
clang++ -Xclang -flto-visibility-public-std -o test.exe test.cpp
Edit:
Or use clang-cl instead:
clang-cl -o test.exe test.cpp

Related

how to solve this problem compiler error that linker command failed in VSCODE

I wrote some C code on VSCODE like that.
The code is divided into three files: the header, the function, and main in same project folder.
But when I started compile, files are can't compile and error. like terminal text.
Maybe I think this error is linking error..
How to solve this problem..?
[source code]
mysqrt.c
#include "mysqrt.h"
#include <stdio.h>
#include <math.h>
double mysqrt(double a, double b){
double result = sqrt(pow(a,2)+pow(b,2));
return result;
}
mysqrt.h
#include <stdio.h>
double mysqrt(double a, double b);
mysqrtTest.c
#include <stdio.h>
#include <math.h>
#include "mysqrt.h"
void main(void){
double sum = mysqrt(3,4);
printf("%.2f\n",sum);
}
[terminal text]
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: warning:
return type of 'main' is not 'int' [-Wmain-return-type]
void main(void){
^
/Users/kim_donggyun/Desktop/My File/MyFile/VSCodeWorkFolder/2019_2_finalExam/mysqrtTest.c:5:1: note: change
return type to 'int'
void main(void){
^~~~
int
1 warning generated.
Undefined symbols for architecture x86_64:
"_mysqrt", referenced from:
_main in mysqrtTest-45c3c1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[OS]: macOS Mojave 10.14.6
to resolve your linking issue you need to compile all your c file, as dependencies are not automatically resolved ( header files could be name separately from code file so .h files and .c files are independant ).
# assuming that gcc is your compiler
gcc -Wall -Wextra -Werror -pedantic -o mysqrtTest mysqrtTest.c mysqrt.c -lm
Although I would recommend you to learn about separate compilation, and using a build system, like make
Example of Makefile
mysqrtTest: mysqrtTest.o mysqrt.o
${CC} -o $# $^ -lm
then use make to build your binary

Creating C DLL and using it in Golang?

I want to create DLL in C and use it in Golang.
I used this tutorial to generate dll :
helloWorld.h
#include<stdio.h>
void __stdcall __declspec(dllexport) hello();
helloWorld.c
#include<stdio.h>
#include "helloWorld.h"
__stdcall void hello()
{
printf("Hello World !!");
}
I used this in command Prompt to compile it
g++ -c helloWorld.c
g++ -shared -o helloWorld.dll helloWorld.o -Wl,--out-implib,libhelloWorld.a
I was able to use the generated dll in this C code , named as example.c :
#include<stdio.h>
#include "helloWorld.h"
int main()
{
hello();
}
and compile it using
g++ -c example.c
g++ -o example.exe example.o -L. -lhelloWorld
But while using the DLL in Golang , i am getting error , please help me
test.go
package main
/*
#cgo LDFLAGS: -L. -lhelloWorld
#include "helloWorld.h"
*/
import "C"
func main(){
C.hello()
}
Error :
# command-line-arguments
C:\Users\kumarmoh\AppData\Local\Temp\go-build544493490\b001\_x002.o: In function `_cgo_525f579e070a_Cfunc_hello':
/tmp/go-build/cgo-gcc-prolog:40: undefined reference to `hello'
collect2.exe: error: ld returned 1 exit status
error image
Other info :
go version go1.11 windows/amd64
64-bit OS , x64-based processor , Windows 10
I tried creating dll in Visual studios and it gave me error as explained here .

Run C function with CUDA calls in Delphi program

My objective is to have a Delphi( or freepascal) code, that will call the C function func like this one:
The C/Cuda file:
/* this is the "progcuda.cu" file */
#include <stdio.h>
__global__ void foo(int *a, int *b, int *c, int n){
/*
add all the vector's element
*/
}
void func(int *a, int *b, int *c,int n){
int *da,*db,*dc;
cudaMalloc(&da, n*sizeof(int));
cudaMalloc(&db, n*sizeof(int));
cudaMalloc(&dc, n*sizeof(int));
cudaMemcpy(da,a,sizeof(int)*n,cudaMemcpyHostToDevice);
cudaMemcpy(db,b,sizeof(int)*n,cudaMemcpyHostToDevice);
cudaMemcpy(dc,c,sizeof(int)*n,cudaMemcpyHostToDevice);
foo<<<1,256>>>(da,db,dc);
cudaMemcpy(c,dc,sizeof(int),cudaMemcpyDeviceToHost);
/* do other stuff and call another Host and Device functions*/
return;
}
The pascal main file:
// this is the "progpas.pas" file
program progpas;
{$mode objfpc}{$H+}
uses unitpas;
var
...
begin
...
func(a, b, c, len);
...
end.
The pascal unit file:
// this is the "unitpas.pas" file
unit unitpas;
{$link progcuda.o}
interface
uses ctypes;
procedure func(a, b, c : cpint32 , n:cint32); cdecl; external;
procedure foo(a, b, c : cpint32 , n:cint32);cdecl; external;
implementation
end.
I've found this post Programming CUDA using Delphi or FreePascal
, but it shows more a way to program CUDA in delphi.
I don't want to program CUDA in Delphi, I want to program in CUDA in pure C/C++ code and only call that C function in delphi.
What is the problem?
How can I link the .cu code to the delphi one?
I'm using linux ubuntu 16.04 LTS, but I also have CUDA and VS in windows if necessary.
Note: if you guys could explain in detail how to do it, would help ( new to pascal and linking files )
I've already tried to generate the .o object file and link it in free pascal with
$ nvcc progcuda.cu -c -o progcuda.o then $fpc progpas.pas
but it fails at linking.
Note: I've tried once to link a normal .o generated by C code to pascal code, using gcc and freepascal compiler, and it worked, but if I use nvcc instead of gcc and rename the extension to .cu ( still same code), the linking fails.
note: new account in stack overflow, i cannot repply answers yet.
I don't know anything about Delphi and FreePascal, but I do know about CUDA, C and C++, so maybe my solution will also work for you.
I'll be demonstrating it with a simple problem:
Content of f.cu:
int f() { return 42; }
Content of main.c:
extern int f();
int main() {
return f();
}
The following works:
$ gcc -c -xc f.cu # need -xc to tell gcc it's a C file
$ gcc main.c f.o
(no errors emitted)
Now when we try replacing gcc with nvcc:
$ nvcc -c f.cu
$ gcc main.c f.o
/tmp/ccI3tBM1.o: In function `main':
main.c:(.text+0xa): undefined reference to `f'
f.o: In function `__cudaUnregisterBinaryUtil()':
tmpxft_0000704e_00000000-5_f.cudafe1.cpp:(.text+0x52): undefined reference to `__cudaUnregisterFatBinary'
f.o: In function `__nv_init_managed_rt_with_module(void**)':
tmpxft_0000704e_00000000-5_f.cudafe1.cpp:(.text+0x6d): undefined reference to `__cudaInitModule'
f.o: In function `__sti____cudaRegisterAll()':
tmpxft_0000704e_00000000-5_f.cudafe1.cpp:(.text+0xa9): undefined reference to `__cudaRegisterFatBinary'
collect2: error: ld returned 1 exit status
The problem here is that nvcc adds references to some symbols from the CUDA runtime API when compiling f.cu, and these symbols have to be linked to the final executable. My CUDA installation is in /opt/cuda, so I will use that, but you have to replace it with wherever CUDA is installed on your system. So if we link libcudart.so when compiling the library we get:
$ nvcc -c f.cu
$ gcc main.c f.o -L/opt/cuda/lib64 -lcudart
/tmp/ccUeDZcb.o: In function `main':
main.c:(.text+0xa): undefined reference to `f'
collect2: error: ld returned 1 exit status
This looks better, no strange errors, but it's still not finding the function f. That's because nvcc is treating f.cu as a C++ file, so it does name mangling when creating the object file, and we have to specify that we want f to have C, and not C++ linkage (see more here: http://en.cppreference.com/w/cpp/language/language_linkage).
To do that we have to modify f.cu like this:
extern "C" int f() { return 42; }
Now when we do:
$ nvcc -c f.cu
$ gcc main.c f.o -L/opt/cuda/lib64 -lcudart
(no errors emitted)
I hope you manage to modify this to work with your language.
EDIT: I tried a bit more complicated example:
// f.cu
#include <stdio.h>
__global__ void kernel() {
printf("Running kernel\n");
}
extern "C" void f() {
kernel<<<1, 1>>>();
// make sure the kernel completes before exiting
cudaDeviceSynchronize();
}
// main.c
extern void f();
int main() {
f();
return 0;
}
When compiling it I got:
f.o:(.data.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
To fix it you also need to add the standard C++ libraries to the linker flags:
$ nvcc -c f.cu
$ gcc main.c f.o -L/opt/cuda/lib64 -lcudart -lstdc++
$ ./a.out
Running kernel
I fixed the files as #Goran Flegar explained:
Add extern "C" int func(...); to the .cu file. And then tried to compile/link the .cu code, but with no device calls (yet with device code), and all worked well.
but when i add a device call ( foo<<<Nb,Nt>>>(...) ) and compile with:
$nvcc progcuda.cu -c
$fpc progpas.pas -ofinal.exe -Fl/usr/local/cuda/lib64
i get:
Free Pascal Compiler version 3.0.4 [2017/12/13] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling prog1.pas
Linking sum.exe
/usr/bin/ld: aviso: link.res contém seções de saída; você se esqueceu -T?
/usr/bin/ld: sum.o: undefined reference to symbol '_Unwind_Resume##GCC_3.0'
//lib/x86_64-linux-gnu/libgcc_s.so.1: error adding symbols: DSO missing from command line
prog1.pas(16,1) Error: Error while linking
prog1.pas(16,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppcx64 returned an error exitcode
So there's still some missing libs.
Solution:
Found that linking the stdc++ and gcc_s lib to pascal solved the compilation problem.
unit unitpas;
// file "unitpas.pas"
{$LINK progcuda.o}
{$LINKLIB c}
{$LINKLIB cudart}
{$linklib stdc++}
{$linklib gcc_s}
interface
uses ctypes;
function func(x,y: cint32): cint32; cdecl; external;
implementation
end.
Run
$nvcc progcuda.cu -c
$fpc progpas.pas -ofinal.exe -Fl/usr/local/cuda/lib64
and everything works.

undefined reference error for linking CUDA static or shared library with gcc

gcc and CUDA question
Hi,
I have compiled a CUDA shared library but can't link it with the main program that uses it. I am compiling the main program with gcc.
The code:
simplemain.c
#include <stdio.h>
#include <stdlib.h>
void fcudadriver();
int main()
{
printf("Main \n");
fcudadriver();
return 0;
}
test.cu
__global__ void fcuda()
{
}
void fcudadriver()
{
fcuda<<<1,1>>>();
}
I compile test.cu as --> It works
nvcc --compiler-options '-fPIC' -o libtest.so --shared test.cu
I compile simplemain.c as ---> It gives error :(
gcc simplemain.c -L. -ltest
/tmp/ccHnB4Vh.o:simplemain.c:function main: error: undefined reference to 'fcudadriver'
collect2: ld returned 1 exit status
try using g++ instead of gcc. nvcc uses c++ style linking conventions. (You don't need to rename any files.)
alternatively, if you must use gcc, preface your void fcudadriver() function definition like this:
extern "C" void fcudadriver()
C and C++ name the functions in different way.
Since nvcc treat the CPU code in .cu file as C++, you could rename your simplemain.c to simplemain.cpp, and compile it with g++
Another solution could be adding extern "C" before the function definition in the .cu file.

What's wrong in creating/using a shared library with gcc here?

In libname.h:
int add_libname(int, int);
In libname.c:
#include "libname.h"
int add_libname(int a, int b)
{
return a+b;
}
I can build the shared library this way:
gcc -shared -fPIC libname.c -o libname.so
But I can't use it in another programe test.c:
#include <stdio.h>
#include "libname.h"
int main(int argc, char* argv[])
{
printf("%d\n", add_libname(1,5));
}
Reporting undefined reference to add_libname when I try to build it..
What's wrong here?
Because add_libname takes (int, int) you're giving it (1+5 = 6) or just (int)
I think you meant
add_libname(1, 5);
Also to compile it correctly you must use gcc like so
gcc -o myapp test.c -L. -lname
the lib part of libname is ignored as it is implicit
To create a shared library use these
gcc -fPIC -c libname.c
it gives warning: position independent code and libname.o file is generated.
and now type these command,
gcc -shared libname.so libname.o
libname.so ( the shared library is created with .so extension). To use the shared library
gcc -I/give the path of libname.h sourcefile.c /give the path of your .so file
example if your c file is file.c and the header file libname.h is in c:\folder1\project and your libname.so (shared library) is in c:\folder\project2
then
gcc -I/cygdrive/c/folder1/project file.c /cygdrive/c/folder/project/libname.so
this is the gcc command to be used while using the shared library.
Thank you.

Resources