enabling pthread on vscode using code runner extension on linux wsl? - visual-studio-code

I have encountered an error on Linux while using Posix thread with Code runner on VS Code and encountered an error of undefined reference to pthread_create();

To fix it please add -pthread argument in code runner settings.json:
"cpp": "cd $dir && g++ $fileName -pthread -o $fileNameWithoutExt &&

Related

Why does VS Code C++ say path not specified?

Looking for help with this error message.
After installing the C++ extension for VS Code, installing MinGW-64, and setting the environment path variable, creating a simple hello world program I am getting the following peculiar error:
C:\Users\oscar\Documents\Labs>cd "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/binc/Users/oscar/OneDrive/Documents/test/" && g++ helloworld.cpp -o helloworld && "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/binc/Users/oscar/OneDrive/Documents/test/"helloworld
The system cannot find the path specified.
Notes:
g++ version shows 8.1.0 and
using CodeRunner extension to run the code in the terminal
Fixed by uninstalling and reinstalling everything

How to compile a C program that includes `math.h` using vscode code-runner extension?

Is there a way to compile a C program that includes math.h using the vscode code-runner extension? I know that the program can be compiled by appending -lm option at the end of the gcc command if I compile it in the terminal. The thing is I do not want to compile the program from the terminal.
As a workaround, I tried to change "code-runner.executorMapByFileExtension" as follows.
"code-runner.executorMapByFileExtension" : {
".c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -lm && $dir$fileNameWithoutExt"
}
Note -lm option appended. But I could not get it done because the command to compile the program is not overwritten?
Any ideas to solve this problem are appreciated.
Edit: Forgot to say that I am on linux.
I haven't been able to figure out a way to load the math.h file in Visual Studio Code, either. It seems that there is no choice but to build from the terminal so that you can pass the -lm switch.

Shortening Run Statement for C++

I recently started using C++ with Microsoft VSCode for competitive programming. Whenever I run the program I have to type
g++ filename.cpp -o filename
then I would type filename in terminal to run the .exe file.
In the context of competitive programming, it can be really time consuming to retype this command. Is there a way to shorten this command?
Also, when I make a change to the program is there a better way of recompiling it instead of making another .exe file?
Any help would be appreciated!
If you are so obsessed with typing filename you can always use the run button in IDE, or just use g++ filename.cpp and it will create a.out file. If you need more, then one version of program, then it is much better to save different cpp files, instead of binaries.
Use coderunner addon for VSCode
In config file, add:
"code-runner.executorMap": {
"c": "cd $dirWithoutTrailingSlash && gcc $fileName -o build/$fileNameWithoutExt && $dirWithoutTrailingSlash/$fileNameWithoutExt",
"cpp": "cd $dirWithoutTrailingSlash && g++ $fileName -o build/$fileNameWithoutExt && $dirWithoutTrailingSlash/$fileNameWithoutExt"
},
"code-runner.saveFileBeforeRun": true,
"code-runner.runInTerminal": true
Click right mouse and run code

Code-runner configuration for running multiple cpp classes in vscode

I have a cpp project with multiple classes and headers. I was trying to make it compile and run using tasks and lunch.json but I gave up. I realized that a while ago I had a problem with Python interperter and went to code-runner configuration to change the default interperter when working with Python. But there has to be a way to make code-runner work even in cpp when having multiple classes and header.
This is what I found in the configuration:
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++14 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
I see that only one file gets compiled. What should I add to the code above to make vscode compile all classes?
I change that line to
"code-runner.executorMap": {
"cpp": "cd $dir && g++ -std=c++14 *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
},
Now it works like a charm.

System.DllNotFoundException in pocketsphinx swig Unity windows build

Goal:
Obtain a wrapper dll to use pocketsphinx in a Unity project in Windows.
Problem:
When running the test program, I get an annoying System.DllNotFoundException even though the .so file is in the same directory as the mono program.
Setup and attempted actions:
For starters, I am using Cygwin. I was able to build absolutely everything, sphinxbase and pocketsphinx no problem. Then I went into the swig/csharp directory and attempted make. It does make, a .so file is created. I am using the default Makefile and make in cygwin. Rules 1-3 run perfectly fine, building, linking and everything. So does the pocketsphinx_continuous test.
pocketsphinx.c: ../pocketsphinx.i
mkdir -p gen
swig -I.. -I../../../sphinxbase/swig -I../include -I../../sphinxbase/include \
-csharp \
-dllimport "libpocketsphinxwrap.so" \
-namespace "Pocketsphinx" -o sphinxbase.c \
-outdir gen ../../../sphinxbase/swig/sphinxbase.i
swig -I.. -I../../../sphinxbase/swig -I../include -I../../sphinxbase/include \
-csharp \
-dllimport "libpocketsphinxwrap.so" \
-namespace "Pocketsphinx" -o pocketsphinx.c \
-outdir gen ../pocketsphinx.i
libpocketsphinxwrap.so: pocketsphinx.c
gcc -fPIC pocketsphinx.c sphinxbase.c `pkg-config --libs --cflags pocketsphinx` -shared -o $#
test.exe: libpocketsphinxwrap.so
mcs test.cs gen/*.cs
run: test.exe
MONO_LOG_LEVEL=debug mono test.exe
clean:
rm -rf gen
rm -f libpocketsphinxwrap.so
rm -f pocketsphinx.c
rm -f sphinxbase.c
rm -f test.exe
.PHONY: run clean
When attempting mono test.exe - the following happens:
Unhandled Exception:
System.TypeInitializationException: The type initializer for 'Pocketsphinx.PocketSphinxPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.DllNotFoundException: libpocketsphinxwrap.so
The .so is in the same directory as the test.exe file and is certainly being looked at because when running mono with MONO_LOG_LEVEL=debug, the output is:
Mono: DllImport error loading library 'C:\Users\fgera\Development\Tools\CMUSphinx\pocketsphinx\swig\csharp\libpocketsphinxwrap.so.dll': 'The system cannot find the file specified.
Many times over and over.
Any help will be greatly appreciated. Again, I need a libpocketsphinx.so x86_64 for windows in Unity to interop with the C# files.
Thanks.
--- edit - added relevant snippet. test.cs just calls this method from the wrapper data structure. These files were all generated by SWIG.
[global::System.Runtime.InteropServices.DllImport("libpocketsphinxwrap.so", EntryPoint="CSharp_Pocketsphinx_Decoder_GetConfig")]
public static extern global::System.IntPtr Decoder_GetConfig(global::System.Runtime.InteropServices.HandleRef jarg1);
I doubt Cygwin DLL is compatible with Mono/Unity. You need to create DLL in Visual Studio, then it will work.
Why do you test the so file with mono, while it is actually used in Unity?
Mono: DllImport error loading library 'C:\Users\fgera\Development\Tools\CMUSphinx\pocketsphinx\swig\csharp\libpocketsphinxwrap.so.dll': 'The system cannot find the file specified.
If file libpocketsphinxwrap.so does exist in C:\Users\fgera\Development\Tools\CMUSphinx\pocketsphinx\swig\csharp\, try rename libpocketsphinxwrap.so to libpocketsphinxwrap.so.dll so mono can find it.
See also mono pinvoke: library names and DllNotfoundException.
You can try build pocketsphinx with Visual Studio since it is offically supported.
BTW, I'd rather write the C# wrapper myself instead of using SWIG since there are many traps in P/Invoke and I don't think SWIG can handle all of them.