warning: unused variable ‘_cgo_a’ - go

What is the '_cgo_a' variable?
I'm trying to link a c++ static lib.
greeter.cpp
#include "greeter.h"
#include <iostream>
void
greet()
{
std::cout << "Greetings\n";
}
greeter.h
#ifndef GREETER_H_
#define GREETER_H_
#ifdef __cplusplus
extern "C" {
#endif
void
greet();
#ifdef __cplusplus
}
#endif
#endif
I compiled the above into a static library like so:
$ g++ -c greeter.cpp
$ ar vfx greeter.o -o libgreeter.a
and here's my main.go
package main
// #cgo CFLAGS: -g -Wall
// #cgo LDFLAGS: -L. -lgreeter
// #include "greeter.h"
import "C"
func main() {
C.greet()
}
Then when I do go build that's what I get:
# error
cgo-gcc-prolog: In function ‘_cgo_261f55e693f4_Cfunc_greet’:
cgo-gcc-prolog:46:49: warning: unused variable ‘_cgo_a’ [-Wunused-variable]
My go version: go version go1.12.5 linux/amd64
EDIT:
If I remove the -Wall from the CFLAGS it compiles fine. Still what is the _cgo_a variable and why is it give me an error?

Do not use -Wall in cgo CFLAGS. This is a general issue in Go. Read more in the github thread: https://github.com/golang/go/issues/6883#issuecomment-383800123

Related

Strange behavior with gcc and inline

I want to define an inline function in a header file (.h) which can be included by numerous source files (.c). Here is a minimal example with 1 header and 2 source files:
Header file foo.h
int ifunc(int i);
extern inline
int
ifunc(int i)
{
return i + 1;
}
Source code file: foo.c
#include <stdio.h>
#include "foo.h"
int foo2(int i);
int main()
{
printf("%d\n", foo2(1));
return 0;
}
Source code file foo2.c
#include "foo.h"
int foo2(int i)
{
return ifunc(i);
}
The problem
When I compile with optimization,
gcc -g -Wall -O2 -o foo foo.c foo2.c
$ ./foo
2
everything works fine. However when I turn off optimization, I get this error:
gcc -g -Wall -o foo foo.c foo2.c
/tmp/cc3OrhO9.o: In function `foo2':
foo2.c:5: undefined reference to `ifunc'
Can someone please explain how to fix so that I can run the code with and without -O2? I am using gcc 4.8.5.
if you replace foo.h with
static inline int ifunc(int i)
{
return i + 1;
}
Both will work.
Declaring it extern means it'll be defined somewhere else which in your original example does not happen. And the optimized build doesn't flag as an error because it already optimized it to be inline it but the non-optimized build does not find a definition in any of the .o files (since they were all compiled with ifunc being an extern as defined in foo.h).
Declaring as static inline will ensure that it is local to each file (the downside being that if it does not inline it, you'll end up with each .o that needs it having a local copy, so don't overdo it).

Unable to get the stack trace with a corefile from a cgo routine when using golang

I am using Golang and cgo. When my C code raises an assert(), I am unable to see the stack trace of the C code when using cgo.
Instead, I see the stack trace of the golang runtime that caught the assert.
Here is an example of my C code
#include <stdio.h>
#include <pthread.h>
#include <assert.h>
#include <string.h>
void fn2(char *arg)
{
int stackvar2 = 256;
printf("Argument %s\n", arg);
assert(1 == 2);
}
void fn1(int arg)
{
int stackvar3 = 512;
char var[256];
strcpy(var, "deadbeef");
fn2(var);
}
void *thread(void *arg)
{
printf("Hello from the thread... going in for an assert\n");
fn1(1092);
return NULL;
}
void hello_world(char *str)
{
pthread_t tid;
printf("Hello World from C and here the str is from Go: %s\n", str);
pthread_create(&tid, NULL, thread, NULL);
sleep(100000);
}
Here is my Go code
package main
/*
extern void hello_world(char *str);
#cgo LDFLAGS: -L. -lhello
#cgo CFLAGS: -g3
*/
import "C"
import (
_ "fmt"
)
func main() {
str := "From Golang"
cStr := C.CString(str)
C.hello_world(cStr)
select {}
}
And here is my Makefile
all:
gcc -g3 -O0 -c hello.c
ar cru libhello.a hello.o
go build hello.go
clean:
rm -f *.o hello
Besides the obvious check of ulimit -c, run the go program with GOTRACEBACK=crash. This will print out more information, and allow the program to exit with SIGABRT to trigger a core dump.
Actually I need to add this to my go code: signal.Ignore(syscall.SIGABRT). This allows me to see the stack trace of the C code that crashed.

How to use shared library

These are my C codes simply print “Hello" Message. And I want to make mylib.c as shared library.
[mylib.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
int mylib(){
printf("### Hello I am mylib #####\n");
return 0;
}
[drive.c]
#include <stdio.h>
int mylib();
int main(){
mylib();
return 0;
}
At the firest I compiled mylib.c with folowing command line to make mylib.o
gcc –fPIC –g –c –Wall mylib.c
Then tried to make it shared librarly like this
gcc -shared -Wl,-soname,libmylib.so.1 -o /opt/lib/libmylib.so.1.0.1 mylib.o -lc
And I did ldconfig to update /etc/ld.so.cache
Finaly I compiled drive.c link with mylib but linker showed error
gcc -g -Wall -Wextra -pedantic -I./ -L./ -o drive drive.c –lmylib
/usr/bin/ld: cannot find –lmylib
Dose someone tell me how can I compile it?
In my way, you have to follow some ways to use shared library in C.
At first I have created a header file named "shared_library.h", in this file I have introduced a function named "method" as a function of this library.
The code is following:
/*-------This is starting of shared_library.h file-----------*/
void method();
/*-------------This is ending of shared_library.h file--------*/
Then I have defined the method in another file named "shared_library.c". The definition as in code is:
/*-------------This is starting of shared_library.c file---------*/
#include "shared_library.h"
void method()
{
printf("Method is called");
}
/*-------------This is ending of shared_library.c file---------*/
And finally, the header "shared_library.h" is ready to use. I use the library in my main C file named "main.c". The contents of "main.c" are as follows:
/*-------------This is starting of main.c file----------------*/
#include <stdio.h>
#include "shared_library.h"
int main()
{
method();
return 0;
}
/*-------------This is ending of main.c file----------------\*/
I found this article ld cannot find an existing library.
It works if I change to gcc -g -Wall -Wextra -pedantic -I./ -L/opt/lib -o drive drive.c –l:libmylib.so.1

CUDA and Thrust library: Trouble with using .cuh .cu and .cpp files together with -std=c++0x

I want to have a .cuh file where I can declare kernel functions and host functions as well. The implementation of these functions will be made inside the .cu file. The implementation will include the use of the Thrust library.
In the main.cpp file I would like to use the implementation that is inside the .cu file. So let's say we have something like this:
myFunctions.cuh
#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/remove.h>
#include <thrust/host_vector.h>
#include <iostream>
__host__ void show();
myFunctions.cu
#include "myFunctions.cuh"
__host__ void show(){
std::cout<<"test"<<std::endl;
}
main.cpp
#include "myFunctions.cuh"
int main(void){
show();
return 0;
}
If I compile by doing this:
nvcc myFunctions.cu main.cpp -O3
And then run the executable by typing ./a.out
The test text will be printed.
However, if I decide to include -std=c++0x by using the following command:
nvcc myFunctions.cu main.cpp -O3 --compiler-options "-std=c++0x"
I get a lot of errors, some of which are the following:
/usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h(159): error: identifier "nullptr" is undefined
/usr/include/c++/4.6/x86_64-linux-gnu/./bits/c++config.h(159): error: expected a ";"
/usr/include/c++/4.6/bits/exception_ptr.h(93): error: incomplete type is not allowed
/usr/include/c++/4.6/bits/exception_ptr.h(93): error: expected a ";"
/usr/include/c++/4.6/bits/exception_ptr.h(112): error: expected a ")"
/usr/include/c++/4.6/bits/exception_ptr.h(114): error: expected a ">"
/usr/include/c++/4.6/bits/exception_ptr.h(114): error: identifier "__o" is undefined
What do these errors mean and how can I avoid them?
Thank you in advance
If you look at this specific answer, you'll see the user is compiling an empty dummy app with the same switch you are using and getting some of the exact same errors. If you restrict the usage of that switch to compiling .cpp files, you'll probably have better results:
myFunctions.h:
void show();
myFunctions.cu:
#include <thrust/sort.h>
#include <thrust/device_vector.h>
#include <thrust/remove.h>
#include <thrust/host_vector.h>
#include <thrust/sequence.h>
#include <iostream>
#include "myFunctions.h"
void show(){
thrust::device_vector<int> my_ints(10);
thrust::sequence(my_ints.begin(), my_ints.end());
std::cout<<"my_ints[9] = "<< my_ints[9] << std::endl;
}
main.cpp:
#include "myFunctions.h"
int main(void){
show();
return 0;
}
build:
g++ -c -std=c++0x main.cpp
nvcc -arch=sm_20 -c myFunctions.cu
g++ -L/usr/local/cuda/lib64 -lcudart -o test main.o myFunctions.o

Link Object File GCC/G++

I have my Main C++ Class main.cpp...
#include "fs.h"
int main(void)
{
return minit();
}
The fs.h:
#ifndef __FS__
#define __FS__
int minit (void);
#endif
And a fs.o (with minit() into) file that is already an object file, compiled with g++ without -g.
Here is my makefile:
myfs: main.o fs.o
g++ -o myfs -m32 -Wall fs.o main.o
main.o: main.cpp fs.h
g++ -o main.o -m32 main.cpp
Every time I try to link everything, the linker says that in main.cpp there is a undefined reference to minit(); What could it be?
You have defined a function called minit() within actually IMPLEMENTING it - that is why you are having this problem.
You need to actually write the function minit():
int minit(void) {
return 0;
}
For example...
You should point it to compiler that the function is defined elsewhere. Try changing this declaration:
int minit (void);
to this
extern int minit (void);

Resources