compiling cuda with gcc results in: error: ‘threadIdx’ undeclared - c

How can I force GCC to compile with threadIdx lines in my code?
I'm attempting to compile my cuda application with a c wrapper.
To generate the .so file I run:
nvcc -arch=sm_11 -o libtest.so --shared -Xcompiler -fPIC main.cu
Then I attempt to compile the c wrapper with:
gcc -std=c99 -I/usr/local/cuda/include -o main -L. -ltest main.c
This results in the following error a few layers down in my code(an include to an included file):
error: ‘threadIdx’ undeclared
Note: Everything does compile and run just fine as a GPU application without any C wrapping.

Gcc has no capability to compile the cuda part of the code. You need nvcc to compile the code. gcc does not know what to do when it came across threadIdx is.

Related

Linking portaudio into a C program on Linux

Problem with linking portaudio into an c program on Linux.
System: Linux Ubuntu 20.4 i5 16 GB
ALSA and pulseaudio were preinstralled.
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
gcc -Wall wm_1.c -lm libportaudio.a -o wm_1
The linker gives me more than 100 error messages all of type "undefined reference"
Here 2 examples out of >100
/home/max/Desktop/dev/portaudio/src/hostapi/alsa/pa_linux_alsa.c:504: undefined reference to snd_pcm_status_get_delay' /home/max/Desktop/dev/portaudio/src/hostapi/oss/pa_unix_oss.c:1778: undefined reference to __pthread_unregister_cancel'
So its obvious that the named parameter/function can not be found.
The error messages all point to source files in the source directory (the directory of the portaudio
package I downloaded to creatie the libs which were all created without error.
The libs are in /usr/local/..
libportaudio.a libportaudio.la libportaudio.so libportaudio.so.2 libportaudio.so.2.0.0 pkgconfig python3.8
and I copied libportaudio.a into the project directory. The lib has a a size of 1.1 MB .
if I use the dynamic libportaudio.so I get the error messages at run time.
I suspect that something went totally wrong with creating the libraries but I have no idea how to solve that
Other option:
Linking parameter or files missing ?
Header file ?
The same program compiles, links and runs without any problem on a iMac OS 10.13.6
where I used the dynamic lib .dylib.
gcc -v wm_1.c libportaudio.dylib -o wm_1
From the documentation:
Note that you will usually need to link with the approriate libraries that you used, such as ALSA and JACK, as well as with librt and libpthread. For example:
gcc main.c libportaudio.a -lrt -lm -lasound -ljack -pthread -o YOUR_BINARY
A little googling goes a long way...
This works:
gcc -Wall wm_1.c -lm libportaudio.a -lasound -pthread -o test.
gcc main.c libportaudio.a -lrt -lm -lasound -ljack -pthread -o YOUR_BINARY
I used that page and the command line at the begin using all 3 parameter but got errors, probably of misspelling, so I gave up on that (also because on the Mac OS it was not necessary). It now links without errors using -lasound and -pthread only (-pthread alone gives still errors and the use/not use of -ljack makes no difference).
I get some errors when I run the program but probably because of missing or wrong ALSA parameter settings. I found -pthread but I could not find -ljack and -lasound.
So the question: what are this 2 parameter doing?
It must be link parameter, but where is the documentation, I searched ld and gcc and did not find anything, while -pthread is documented.

Cannot find -lCommunication collect2: error: ld returned 1 exit status

I do not know gcc and c well. In my /home/pi/Desktop/intern/adis16227_generic directory I have following 5 files.
ADIS16227.c
ADIS16227.h
Communication.c
Communication.h
main.c
main.c
#include<stdio.h>
#include "Communication.h" // Communication definitions.
int main() {
printf("hello!!\n");
unsigned char status = 0;
status = SPI_Init(0, 1000000, 1, 1);
printf("%u", status);
return 0;
}
Run command:
$ sudo gcc -L /home/pi/Desktop/intern/adis16227_generic main.c -lCommunication
Error:
/usr/bin/ld: cannot find -lCommunication
collect2: error: ld returned 1 exit status
Question:
What I am missing here?
What do I need to run the code?
-l is for libraries, and you never built a library from your Communication.c. The simplest solution is just add Communication.c to your compiler command line.
For larger projects, compile each translation unit separately with the -c switch like this:
gcc -c -Wall -Wextra -pedantic -omain.o main.c
gcc -c -Wall -Wextra -pedantic -oCommunication.o Communication.c
and so on ... (as a suggestion, I added some common warning options here, they help you spot errors)
The resulting .o files are object code. That's already compiled machine code, but with meta-information needed for a linker to link it with other object code into a complete executable.
Then link them all with one command:
gcc -oprogram main.o Communication.o
If you actually want a library from -- say -- Communication.c and ADIS16227.c, you could compile both to object code:
gcc -c -Wall -Wextra -pedantic -oCommunication.o Communication.c
gcc -c -Wall -Wextra -pedantic --oADIS16227.o ADIS16227.c
and then use ar to create a static library from them:
ar rcs libCommunication.a Communication.o ADIS16227.o
Then your initial compiler command would work (with the -lCommunication switch).
Final piece of advice: Never compile as root. This is completely unnecessary. So remove your sudo here.
those options:
-L /home/pi/Desktop/intern/adis16227_generic -lCommunication
suggest that the linker should find libCommunication.a (or .so) in the /home/pi/Desktop/intern/adis16227_generic directory.
But there are only sources in this directory. The linker won't build the sources of your "Communication" library for you.
So you could build the library and link with it:
gcc -c ADIS16227.c Communication.c
ar r libCommunication.a ADIS16227.o Communication.o
but maybe the fastest & quickest way to achieve a successful build would be:
sudo gcc -o main *.c
so it compiles all the files of the directory into the executable called main
Of course, it makes compilation times longer, but maybe it's not noticeable.
First move into the /home/pi/Desktop/intern/adis16227_generic directory:
cd /home/pi/Desktop/intern/adis16227_generic
Then, compile the source:
gcc ADIS16227.c Communication.c main.c -I .
You can now run your compiled program (called by default a.out):
./a.out
You have to compile separatedly files and then compile main with related obj file.
gcc -c Communication.c Communication.h
gcc main.c Communication.o -o main

Cannot compile using ALSA

I am trying to create an C application on Debian GNU/Linux which uses the PortAudio interface. To do this I must compile my program with gcc -lrt -lasound -ljack -lpthread -o YOUR_BINARY main.c libportaudio.a from this docs.
For this I installed libasound2-dev, and I checked where the files are using apt-file search libasound.so, this is the output:
lib32asound2: /usr/lib32/libasound.so.2
lib32asound2: /usr/lib32/libasound.so.2.0.0
lib32asound2-dev: /usr/lib32/libasound.so
libasound2: /usr/lib/x86_64-linux-gnu/libasound.so.2
libasound2: /usr/lib/x86_64-linux-gnu/libasound.so.2.0.0
libasound2-dev: /usr/lib/x86_64-linux-gnu/libasound.so
So the libasound should be installed correctly, but when I compile my program with this makefile:
DMXTest: main.c libdmx.a
gcc -static -Wall main.c -L. -ldmx -lusb -lrt -lasound -ljack -lfftw3 -g -o main libportaudio.a
I get the following error: /usr/bin/ld: cannot find -lasound.
How can I link this library correctly?
You don't have libasound.a for -static, you will need that, or you can almost certainly just remove -static from the Makefile (likely in LDFLAGS or CFLAGS).
There's is a related Debian bug 522544, and a related Ubuntu bug #993959.
You may be able to build your own libasound from source, though as it also uses other libraries (notably libpthread.so, librt.so and libdl.so) I suspect it may remove some functionality when you build it statically, though it's supported with ./configure --enable-static at build time
(or try --enable-shared=no --enable-static=yes).
FWIW, the use of static binaries is "discouraged" by the glibc maintainers, though I don't agree...
To compile my code i used the following command
gcc -o rec_mic rec_mic.c -lasound
and it works perfectly, without create my own static library.

compiling a c program to use in python code

I have a c program that I need to compile and use in the middle of a python code. it is not c++ program so I assume I should change this syntax for compiling because I get a lot of errors related to not having "main()" in the program which is just because it is a c (not c++)for a x86_64 Mac system?
how should I change this syntax for a program.c code?
g++ -o program.x program.c
Your C code should be compiled into a shared library:
gcc -shared -o program.so -Wall -Wextra -fPIC -O2 -DNDEBUG program.c
And then you can use ctypes module to invoke functions in your library from Python.

Alchemy C Code compilation

I am using alchemy to compile the C code.
This is the way I am compiling
gcc oggvorbis.c -O3 -Wall -swc
oggvorbis.swc -lvorbis
I am getting an error
llvm-gcc: oggvorbis.swc: No such file
or directory.
But the command works fine when i don't use any shared library.
Your command line invocation should probably read (untested)
gcc oggvorbis.c -O3 -Wall -swc -o oggvorbis.swc -lvorbis

Resources