Ok so I am having an issue with errors in VSCode. Basically I decided to reorganize and move my header files into a separate folder, "include". My directory put simply is as follows:
-build
-include
|-SDL2
|-SDL2_Image
|-someHeaderFile1.h
|-someHeaderFile2.h
-src
|-main.cpp
|-someCppFile.cpp
-Makefile
My Makefile contains:
SRC_DIR = src
BUILD_DIR = build/debug
CC = g++
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJ_NAME = play
INCLUDE_PATHS = -Iinclude -I /include
LIBRARY_PATHS = -Llib
COMPILER_FLAGS = -std=c++11 -Wall -O0 -g
LINKER_FLAGS = -lsdl2 -lsdl2_image
all:
$(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)
The program compiles and runs, however, my issue is with VSCode as it shows an error having the include as : #include "someHeaderFile1.h" vs #include "../include/someHeaderFile1.h"
Any assistance would be appreciated.
You need to put that folder's path to the Include path. One way to do that is shown below. The screenshots are attached with each steps so that it(the process) would be more clear.
Step 1
Press Ctrl + Shift + P
This will open up a prompt having different options. You have to select the option saying Edit Configurations
Step 2
After selecting Edit Configurations a page will open with different options. You have to scroll down and go the the option saying Include Path and just paste the path to your include folder there.
Below is the picture after adding the include folder's path into the Include Path option.
Step 3
Now after adding the path to the include folder into the Include path field you can close this window and all the vscode errors that you mentioned will not be there anymore.
If you have install Microsoft C/C++ extension properly, and the directory you show is the root path of your VSCode workspace, you can add Include path options in C/C++: Edit configurations (UI), or edit .vscode/c_cpp_properties.json like:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
// Add your custom include path here
"${workspaceFolder}/include/**",
],
"defines": [],
"compilerPath": "/usr/bin/g++",
// ...other options
}
],
"version": 4
}
For more details refer to the document.
Related
Ok so I am having an issue with errors in VSCode. Basically I decided to reorganize and move my header files into a separate folder, "include". My directory put simply is as follows:
-build
-include
|-SDL2
|-SDL2_Image
|-someHeaderFile1.h
|-someHeaderFile2.h
-src
|-main.cpp
|-someCppFile.cpp
-Makefile
My Makefile contains:
SRC_DIR = src
BUILD_DIR = build/debug
CC = g++
SRC_FILES = $(wildcard $(SRC_DIR)/*.cpp)
OBJ_NAME = play
INCLUDE_PATHS = -Iinclude -I /include
LIBRARY_PATHS = -Llib
COMPILER_FLAGS = -std=c++11 -Wall -O0 -g
LINKER_FLAGS = -lsdl2 -lsdl2_image
all:
$(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)
The program compiles and runs, however, my issue is with VSCode as it shows an error having the include as : #include "someHeaderFile1.h" vs #include "../include/someHeaderFile1.h"
Any assistance would be appreciated.
You need to put that folder's path to the Include path. One way to do that is shown below. The screenshots are attached with each steps so that it(the process) would be more clear.
Step 1
Press Ctrl + Shift + P
This will open up a prompt having different options. You have to select the option saying Edit Configurations
Step 2
After selecting Edit Configurations a page will open with different options. You have to scroll down and go the the option saying Include Path and just paste the path to your include folder there.
Below is the picture after adding the include folder's path into the Include Path option.
Step 3
Now after adding the path to the include folder into the Include path field you can close this window and all the vscode errors that you mentioned will not be there anymore.
If you have install Microsoft C/C++ extension properly, and the directory you show is the root path of your VSCode workspace, you can add Include path options in C/C++: Edit configurations (UI), or edit .vscode/c_cpp_properties.json like:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
// Add your custom include path here
"${workspaceFolder}/include/**",
],
"defines": [],
"compilerPath": "/usr/bin/g++",
// ...other options
}
],
"version": 4
}
For more details refer to the document.
I've got an existing project written in C++ from my predecessor. He used CodeBlocks as an IDE.
I never used this IDE and hardly programmed any C++. He used the tinyxml library, so I added it to the project.
When I try to build it, the build log says something like this:
g++.exe -Wall -fexceptions -DBOOST_THREAD_USE_LIB=1 -D_WIN32_WINNT=0x0501
-Wall -IC:\somepath\boost_1_74_0 -Iinclude -Isrc -IZ:\tinyxml
-c C:\project\AsyncSerial.cpp -o obj\Debug\AsyncSerial.o
Question:
Where is -Isrc -IZ:\tinyxml defined? I don't have any Z:-drive and want to define my own path to the library.
I searched inside the settings and project setup, but didn't find anything like that.
Edit: same goes for:
g++.exe -LC:\somepath\boost_1_74_0\stage\lib -o bin\Debug\project.exe obj\Debug\AsyncSerial.o obj\Debug\main.o obj\Debug\tinyxml\tinystr.o obj\Debug\tinyxml\tinyxml.o obj\Debug\tinyxml\tinyxmlerror.o obj\Debug\tinyxml\tinyxmlparser.o -lws2_32 C:\Programme\boost\boost_1_48_0\stage\lib\libboost_filesystem-mgw46-mt-s-1_48.a C:\Programme\boost\boost_1_48_0\stage\lib\libboost_system-mgw46-mt-s-1_48.a C:\Programme\boost\boost_1_48_0\stage\lib\libboost_date_time-mgw46-mt-sd-1_48.a C:\Programme\boost\boost_1_48_0\stage\lib\libboost_program_options-mgw46-mt-sd-1_48.a C:\Programme\boost\boost_1_48_0\stage\lib\libboost_thread-mgw46-mt-1_48.dll.a C:\Programme\boost\boost_1_48_0\stage\lib\libboost_regex-mgw46-1_48.dll.a
Where does -lws2_32 value come from? I installed boost to the path specified in -L. My boost path was set in global variables and it's the only global variable that is shown.
Edit2:
for information: my path to boost is a relative path in the project folder, and definitely not in program files.
Added pictures of my settings:
Look for the include search directory configuration in
menue "Project"
entry "Build options ..."
tab "Search directories"
subtab "Compiler"
Linker search path nearby in subtab "Linker".
Config of libs to link in tab (not subtab) "Linker settings".
My CodeBlocks is 16.01, but that should not be too different whatever you use.
Quote of OP comment (to make this answer complete):
I found it, they where defined in the Debug/Release Linker settings.
Here "it" refers to the reason/location of the linker paths which did not match the config screenshots in the question.
I built wxWidgets without any errors with make and did make install and now I have the installation in /usr/local/include/wx-3.1/. Both wx/wx.h and wx/setup.h are present in that folder. I'm using the latest VS Code with the latest Ubuntu 20.04.
c_cpp_properties.json file:
"configurations": [
{
"name": "Linux",
"includePath": [
"/usr/local/include/wx-3.1/",
"${default}"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu18",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
main.cpp:
#include "wx/wx.h"
int main(){}
Compiling gives the following error:
g++ -std=c++17 -Wall -Wextra -g -c -o src/main.o src/main.cpp
src/main.cpp:1:10: fatal error: wx/wx.h: No such file or directory
1 | #include "wx/wx.h"
| ^~~~~~~~~
compilation terminated.
The weird thing is that VS Code Intellisense detects that wx is included and there is no error or red underline (removing the include folder from includePath makes VS Code display an error).
What's the problem here? Everything seems to be correctly included.
in addition you will need to point the linker to the libraries to use with the -L option and use -l (lowercase L) with every library you will need.
Example:
-L/usr/local/lib -lwxcore-3.1 -lwxbase-3.1
Check the library names there and use them without lib prefix.
Better yet - try to run wx-config --libs and use the output for the linker options in your IDE.
Also, for complete set of options to the compiler use the output of wx-config --cxxflags
You must configure the IDE to call the compiler with the right options. The right options for wxWidgets projects must include the output of `wx-config --cxxflags` for C++ compiler and `wx-config --libs` for the linker.
Do not hardcode any paths in your project options, using wx-config is both simpler and more portable.
I'm trying to write a GN file that generates .gch files. I've gone through the GN documentation here
I just don't understand how to implement this, i am novice when it comes to GN. So let me explain what i'm trying to achieve.
I want GN to create the following pair of compiler commands:
my-clang -x c++-header pch/hello_world.h -o pch/hello_world.h.gch
my-clang -include pch/hello_world.h -o hello.out -c hello_world.cpp
I expected to be able to create a source set in my GN file like this:
source_set("source_set0") {
precompiled_source = "//pch/hello_world.h"
precompiled_header = "../pch/hello_world.h"
cflags = [
"-include$precompiled_header"
]
sources = [
"//hello_world.cpp",
]
}
This however does not actually generate the pre-compiled header.
I expect I need to have another source_set or equivalent to specifically compile the header file, however my understanding is that the .gch suffix is required for the compiler to recognise the file as a pre-compiled header.
So i figured that i'd need to convince GN to create the specific output file based on the target
hello_world.h file output hello_world.h.gch
hello_world.cpp output hello_world.o
Ideally i'd like to be able to modify my tool specification:
tool("cxx") {{
command = "\"{cxx_exe}\" ... -c {{{{source}}}} -o {{{{output}}}}"
outputs = [
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.o",
**SOME CONDITION**
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.h.gch",
]
}}
However my reading of Tool Variables indicates that the above functionality is only available for linker_tools.
It seems as well that GN needs some convincing to associate cxx tool with .h files.
Has anyone got any experience with this or able to point me in the right direction.
TL;DR
I want to compiler header files with GN to generated .gch file
I want to use the gch in later step of compile
Following the documentation didn't help, am i misunderstanding
How to convince GN to compiler .h files
Is there i need to make sure that the compilation of the header file happens first
Thanks for your time
p.s.
cpp file contents:
int main (void){
const char* greeting = "Hello world";
print_greeting(greeting);
}
.h file contents
#pragma once
#include <iostream>
void print_greeting(const char* greeting){
std::printf("%s", greeting);
}
I got to the bottom of this, I modified the tool:
tool("cxx") {{
command = "\"{cxx_exe}\" ... -c {{{{source}}}} -o {{{{output}}}}"
precompiler_header_type = "gcc"
outputs = [
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.o",
**SOME CONDITION**
"{{{{source_out_dir}}}}/{{{{target_output_name}}}}.{{{{source_name_part}}}}.h.gch",
]
}}
Then update my gn file:
config("precompiled_header"){
precompiled_header="../pch/hello_world.h"
precompiled_source="//pch/hello_world.h"
}
source_set("source_set1") {
public_configs = [
":precompiled_header"
]
sources = [
"//hello_world.cpp",
]
}
In hindsight this is actually what the documentation said to do. The command lines that are created by GN now look like this
ninja.exe -t commands
"my-clang++.exe" ... -O2 -g -x c++-header -c ../pch/hello_world.h -o obj/pch/source_set1.hello_world.h-cc.gch
"my-clang++.exe" ... -O2 -g -include obj/pch/source_set1.hello_world.h-cc -c ../hello_world.cpp -o obj/source_set1.hello_world.o
Hope this helps someone in the future
In Eclipse platform (Windows 10, MinGW toolchain), I'm trying to create a makefile to compile a project with .cpp and .hpp files.
To do that, I choose to create a new project:
File ->
(popup) New Project
C/C++ -> C/C++ Project => NEXT -> C++ Managed Build => MakefileProject -> Hello World C++ MakefileProject
This example compiles and links perfectly.
But I want to move the .cpp file into a src directory, then in the Project Explorer window, over the name of the project, I select to create a "Source
Folder", naming it as "src". Then I move the .cpp file inside the directory, and try to build the project without success.
How must I modify the makefile included with this example? I've tried everything but without luck. I get this error:
*19:51:33 **** Incremental Build of configuration Default for project CE_SEP ****
make all
make: *** No rule to make target CE_SEP.o', needed byCE_SEP.exe'. Stop*
Thanks for your support in advance!
Stop depending on an IDE. There is an actual "Makefile" that you can go in and edit. You likely just have to modify the path to the source file in the Makefile and/or add another Makefile in the "src" folder.
If you don't know Makefile syntax, google a brief tutorial.
Or save yourself some time in the long run and just learn CMake which is just a scripting type language to write platform-portable build systems -- it will generate Makefiles, MSVC projects, Xcode projects, Ninja build files, etc.
I have solved it. I had written wrongly the name of the file, and I have modified the Makefile adding "src\" before the name of the object an exe file:
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = src\CE.o
LIBS =
TARGET = src\CE.exe
$(TARGET): $(OBJS)
$(CXX) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
But I rather will learn some of CMake as "jonrobm" suggested me.
Thank you again!