How to configure VSCode task to compile multiple files on Linux? - c++

I have a project to compile in VSCode that consists of multiple source files and want to add a single argument to my task that would tell g++ to link all files with .cpp extension. The following does not work:
"args": [
"${workspaceFolder}/*.cpp*",
...
g++: error: /home/kirusfg/Study/Programming/CSCI 152/Assignments/Assignment 4/*.cpp*: No such file or directory,
even though
g++ '/home/kirusfg/Study/Programming/CSCI 152/Assignments/Assignment 4/*.cpp*' -o main-optimized.out -g -O5 -flto -Wreturn-type -pedantic -pedantic-errors -Wundef -std=c++17
works perfectly if entered via terminal.

Seems to me you are having issues with space in your 'workspaceFolder' path, "args" field not handling spaces well.
It seems like your best option is to add the arg to the "command" field.
i.e.:
...
"command": "g++ ${workspaceFolder}/*.cpp*",
"args":[],
...
I hope it will fix your issue.
Reference:
See closed issue from VScode GitHub: How to pass arguments with space to tasks correctly

Related

Create Makefile to be used in different environments for C++

I would like to compile and run my program in two different environments. The libraries in both environments are installed on slightly different places, resulting in different makefile-lines:
In makefile A:
CXXFLAGS=-I$(DIR) -flto -fopenmp -O3 -g -march=native -std=gnu++17 -c -I/opt/interp2d/include -std=c++17 -I/opt/splinter/include -I/usr/include/eigen3
In makefile B:
CXXFLAGS=-I$(DIR) -nostindc++ -I~/local_opt/eigen/include/eigen3/ -I~/local_opt/boost/include -I~/local_opt/armadillo/include -flto -fopenmp -O3 -g -march=native -std=gnu++17 -c -I~/local_opt/interp2d/include -std=c++17 -I~/local_opt/splinterp/include -I/usr/include/eigen3
My problem now is that I am developing the program on the first machine, using makefile A, but also deploying it on the second machine. The deployment is done using git.
Every time I do a git pull on the second machine, I have to fix all the paths in the makefile in order to compile the program properly. Nevertheless I still would like to include the makefile in the git repository in order to keep both makefiles at the same level regarding compiling flags and linked libraries.
Thus, is there an easier way to still sync the makefile via git, while using different paths for the libraries and includes?
I think you could solve your problem by conditionally setting the variable CXXFLAGS in a common file (e.g.: config.mk) and by including that file in your makefiles.
The value used for setting the CXXFLAGS variable could, for example, depend on the value of the environment variable HOST:
ifeq ($(HOST),A)
CXXFLAGS = ... # for machine A
else # B
CXXFLAGS = ... # for machine B
endif
Then, include this config.mk makefile in both makefileA and makefileB:
include config.mk
I like this answer, however, I thought I'd mention this for completeness: If you have a lot of different hosts you can do something to the effect of:
include HostConfig_$(HOST).mk
And then create HostConfig_A.mk and HostConfig_B.mk which set host specific flags (Be it directories, etc). This is useful if you are managing a large project with lots of different host-specific variables.
As well, (for smaller projects), you could do something to the effect of:
CXX_INCLUDES_A = ...
CXX_INCLUDES_B = ...
CXX_FLAGS := -I$(DIR) -flto -fopenmp -O3 -g -march=native -std=gnu++17
CXX_FLAGS += $(CXX_INCLUDES_$(HOST))
The traditional answer to this problem is a configure script (see automake, autoconf for widely used framework). After checking out the source you run ./configure --with-eigen=~/local_opt/eigen/include/eigen3/ and it will adjust your Makefiles accordingly (usually generates Makefile from Makefile.in and only Makefile.in is in git).
Note: Properly done you only need to run configure on the first checkout, not on updates. make can generate Makefile again automatially as needed.

Building a C++ formula for homebrew

I have very very little experience with Ruby and I was trying to build a simple homebrew formula. I had a simple test project with the following structure
.
├── Makefile
└── test.cpp
0 directories, 2 files
And then I have the following .rb formula file
class Testbrew < Formula
desc ""
homepage ""
url ""
version ""
head ""
sha256 ""
def install
# system "make"
# system("g++ -std=c++14 -O3 test.cpp -o testbrew")
system "g++", "-std=c++14",
"-O3",
"-Wall",
"-Werror",
"-Wextra",
"-pedantic",
"-Wvla",
"test.cpp",
"-o testbrew"
bin.install "testbrew"
ohai("Done!")
end
end
The confusing part here is that the first two commented lines of system work to install the package but the third does not, any idea why? If I try the third line I get the error
==> g++ -std=c++14 -O3 -Wall -Werror -Wextra -pedantic -Wvla test.cpp -o testbrew
Error: No such file or directory - testbrew
Also as a followup, are there certain things that the function call system() does not allow the user to do? i.e. are there any security restrictions imposed (for example with ptrace)?
Your argument "-o testbrew" is wrong. That parses as -o with the option " testbrew" including the space. This is only possible because system with mutiple arguments bypasses the normal shell parsing, you're directly responsible for correctly splitting arguments.
You've broken the other arguments out correctly. I'd recommend doing it that way:
"-o", "testbrew"

Can't add compiler options for nvcc in nsight eclipse

How do I add options to my nvcc using nsight eclipse. I tried to modify the command option Under Project->Properties->Build->Settings->Tool Settings-> NVCC Compiler. I changed it from "nvcc" to "nvcc --someoption". However when it compiles I see this output "/usr/local/cuda-7.0/bin/nvcc -O3 -ccbin gcc-4.9 -std=c++11 -gencode arch=compute_35,code=sm_35 -gencode arch=compute_52,code=sm_52 -odir "." -M -o "binomial.d" "../binomial.cu"
Notice that --someoption in not in it. How can I add an option in eclipse? I also noticed that I can change the command from nvcc to some gibberish and it still compiles so I think that option does not affect anything. If so how can I add compiler options which eclipse does not include in its gui.
I would recommend using the -optf switch that is selectable under Project Settings... Build ... Settings...Tool Settings...Miscellaneous and add your own file to the project that contains whatever compiler switches you want to add.
I think most compiler switches are already covered in the GUI, however.

Configuring g++ from Code::Blocks doesn't take affect on command line

I'm trying to change the settings of g++ from the Code::Blocks IDE. I went to the Settings tab, clicked Compiler... and checked various options for the compiler to use, like
Enable all warnings (-Wall)
Have g++ follow the C++11 ISO C++ language standard (-std=c++11)
......
These are just two of many others; when I compile on the command line, here is what comes up:
g++ -o example example.cpp
# warning: initializer lists only available with -std=c++11 ...
Notice how there's no warning either - I have an unused variable in my program. It only works if I give the options manually:
g++ -Wall -std=c++11 -o example example.cpp
Do you think I might have done something wrong when setting up the compiler? Why aren't the options taking affect?
Invoking the compiler from the ide is completely independent from doing it in a command line shell. There's no reason for the setting and usage of one to have any effect on the other.

QT5 attaching project name with every sourcefile name, compiling error

i want to get started with QT. I donwloaded QT5 MINGW compiler with QT creator and i am trying to build the pre attached example named affine the problem is that the QT5 i think embed the project name with each of source file and thus gives error that file not found. some thing similar
:-1: error: ..affinemain.cpp: No such file or directory
while the file name is just
main.cpp
i don't know how to fix it. I searched lot on internet but could not found anything useful.
I even try to compile from command prompt but i am not fimmiliar with command prompt compiling as i am new to QT and previously i am totally developed with IDE in visual studio and eclipse for java so i have no idea about the make file and compiler command line arguments.
could some body please help me to fix this issue and can you tell please why compiler attaching project name with the source file name?
Thanks in advance
I have got the same problem and my solution may help you.
I am working with Qt5.0.1 now, and there are two distributions to work on windows with it: Qt5.0.1-mingw and Qt5.0.1-msvc2010.
I had to use mingw and there was a problem on my setup that "/" is ignored in path's.
So according to Qt Creator, compiler was called to process file mainwindow.cpp and this file was passed to it
g++ /*truncated*/ ..\qt-example\mainwindow.cpp
Below is the full compiler input:
g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -DQT_OPENGL_ES_2 -DQT_OPENGL_ES_2_ANGLE -DQT_NEEDS_QMAIN -I..\qt-example -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtWidgets" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtGui" -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtCore" -I"debug" -I"." -I"." -I"..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\mkspecs\win32-g++" -o debug\mainwindow.o ..\qt-example\mainwindow.cpp
And the error produced.
g++.exe: error: ..qt-examplemainwindow.cpp: No such file or directory
g++.exe: fatal error: no input files
compilation terminated.
So, we can see that "\" is ignored by the compiler and file name is merged with directory name.
The solution to that problem goes to the tools that are used - MinGW (Minimalist ports of GCC and Binutils). And also MSYS - a collection of GNU utilities such as bash, make, gawk and grep to allow building of applications and programs which depend on traditionally UNIX tools to be present. In our case - g++.
MSYS is not shipped with Qt5.0.1-mingw and g++ is not using it, but having MSYS available in your PATH environment variable breaks the system.
MSYS is used for git scm, which I have installed, so my path contains links to MSYS that goes bundled with git. So I have next paths in my PATH environment variable.
C:\Program Files (x86)\git\bin;C:\Program Files (x86)\git\cmd
I have not found how MSYS is used by Qt Creator or g++, or where it is linked, but when I have dropped next path from PATH:
C:\Program Files (x86)\git\bin;
and restarted Qt Creator - g++ succeeded on compiling my file, it worked.
The question why/how it influences the Qt Creator/g++ that should not use MSYS utils installed with git is still open.
i can´t comment.
important : delete all the files in the release and debug folder (compiled version) before try the tips of the autor ...