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.
Related
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 &&
I'm working in vscode, and I want to build the chromium in tasks.json, but the build shell report error command not found. I use echo $PATH to see the environments variables in tasks.json. It seems like the build shell in vscode doesn't execute source ~/.bashrc, so it can't find environment variable, but the terminal in vscode is in working order. Could someone help me?
I had the same issue and fixed it with the solution below
Solution For OSX with ZSH
Add a new file under /usr/local/bin/zsh-with-rc
#!/usr/bin/env zsh
source ~/.zshrc
/bin/zsh $#
Then run
chmod +x /usr/local/bin/zsh-with-rc
In settings.json add:
"terminal.integrated.automationProfile.osx": {
"path": "/usr/local/bin/zsh-with-rc",
}
Solution For Linux With Bash
Add a new file under /usr/local/bin/bash-with-profile
#!/usr/bin/env bash
source ~/.bash_profile
source ~/.bashrc
/bin/bash $#
Then run
chmod +x /usr/local/bin/bash-with-profile
In settings.json add:
"terminal.integrated.automationProfile.linux": {
"path": "/usr/local/bin/bash-with-profile",
}
Add "terminal.integrated.shell.linux": "/bin/bash" in settings.json
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.
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
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.