precompiled header in visual studio 2022 - c++

I am pretty new to creating a dll file, and I would like to use a precompiled header, so that I can build faster. I am currently using the Visual Studio 2022.
Here is my directory structure looks like
Foo
|-- include
| |-- pch.h
| |-- globalPch.h
|
|-- src
| |-- pch.cpp
| |-- globalPch.cpp
| |-- dllmain.cpp
| |-- FooDir
| | |-- A.cpp
| | |-- A.hpp
| | |-- B.cpp
| | |-- B.hpp
I have globalPch.h, which has all the standard library and some user typedef, and I have my pch.h to include all globalPch.h. So, I've set up the pch.cpp to create (/Yc) Precompile Header. Then, globalPch.cpp to Use(/Yu), and I realize that I actually put the relative path of pch.h into pch.cpp.
But I am getting an error saying that command-line option was not found in the source file on pch.cpp file. I right-clicked the pch.cpp --> Precompiled Headers --> Precompiled Header File --> set the path to pch.h. I still got the same error as above.
Is there a solution to this? or I looked at some OpenSource code if someone has a precompile header problem like me. But it looks like they've put pch.h and pch.cpp into the same dir. Is this conventional?

Related

how to link and include c++ headers from different directory in vscode

I am new in programming I am using vscode in windows and compiling via Mingw 64. I am requesting an answer to this question after tried of trying for days. I googled it many times. here is how my project tree looks. please help me include and link libA to libB and common.h to all cpp files and libA and libB to to main.cpp. Please answer.
project
|---lib
| |---libA
| | |---header1.h
| | |---header1.cpp
| |
| |---libB
| |---header2.h
| |---header2.cpp
|---common
| |---common.h
| |---common.cpp
|---main.cpp
From VSCode, you can right click on the headers and select Copy Path. That should work when including headers from anywhere on your computer.
As for the .cpp files, you could use relative paths to the directory you're compiling in, when compiling and linking the files, or use the -I option when compiling with g++.
This might help.
You should use the relative path in #include.
For example to import header1 use -#include "./lib/header1.h"
and to import common use #include "./common/common.h"

How to copy all headers from a C++ project with premake

I have a C++ project with its source files (.cpp and .h files) in a directory called src and its subdirectories. I want, once my project is compiled, to copy all the header files from this source folder into another directory, maintaining the folderstructure of src.
I have tried to copy these files with post-build commands:
postbuildcommands
{
"{COPY} src/*.h include"
}
and
postbuildcommands
{
"{COPY} src/**.h include"
}
But these only copy the .h files directly in src and not those in subdirectories. For example, this
src
+-- a.h
+-- a.cpp
+-- sub
| +-- b.h
| +-- b.cpp
becomes
include
+-- a.h
instead of
include
+-- a.h
+-- sub
| +-- b.h
Are you using windows linux or mac?
Or does it need to be cross platform?
It looks like the {copy} token doesn't pass the /s flag to xcopy on windows
https://github.com/premake/premake-core/wiki/Tokens
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy
One possible solution is to find all files or get them from the project somehow and create a postbuild command for each header file which might be a lot slower

Library with sub-libraries and include directories, how do I do it in CMake?

I have a project structured in this way:
project
|\_subdir
| | Some .cxx files
| | some .h files (included with #include "foo.h"
| \_subdir
| | Some .h files (included with #include <subdir/foo.h>)
\_subdir2
| some .cxx files
| some .h files (included with #include "foo.h"
\_subdir2
| Some .h files (included with #include <subdir2/foo.h>)
I need to do a few things here:
The subdir/subdir directories should be publicly available for use in programs that use this library with #include <subdir/foo.h>.
The subdir directories should produce their own statically linked libraries so that I can run tests making sure that the dependency relationships between the different subdir directories are maintained.
The whole thing should produce either a shared or static library that has everything in all the subdir directories and can be linked to by programs using this library.
Currently I have a build system that I've made using autotools (but not automake) plus some custom made perl scripts that process the dependency output from the -M flags and create custom makefiles that express dependency information. Additionally, they create an include directory that's a linkfarm so everybody can use #include <subdir/foo.h> and just -I the linkfarm directory.
I want to convert this to CMake, but I'm at a loss as to how to handle the subdir structure and includes. I can re-arrange the directory structure somewhat if absolutely necessary, but I would very much like to preserve the pseudo-independence of the various subdir directories, partly because it's very important to me to be able to make sure the sub-libraries have a specific dependency relationship as a way to maintain modularity and cleanliness.
How would I do this in CMake? Do I have move the subdir/subdir directories?
Because it looks like if I add the subdir directories as a public include, then things will go really awry as people will see the private .h files at the subdir level as well as being able to include the .cxx files that are in them. Even worse, they will see them as top-level includes (i.e. #include <bar.cxx> where bar.cxx is a private implementation file who's object code should already be in a library).
But if add subdir/subdir as an include directory, the .h files won't appear in subdirectories like they should (i.e. #include <subdir/public.h> won't work, instead I'll have to use #include <public.h> which isn't my intent at all).
If you bother about users of your library, they will see installed files, not ones in the source/build directories. If you don't install private headers (from subdir/), a user won't see them.
But if you do not intend to install library, then you may separate public and private include trees. E.g. each project may have private headers directly in its source directory, and public headers under its include/ subdirectory:
project
|\_subdir
| | Some .cxx files
| | Private .h files (included with #include "foo.h")
| \_include (public include directory)
| \_subdir
| | Public .h files (included with #include <subdir/foo.h>)
\_subdir2
| some .cxx files
| Private .h files (included with #include "foo.h")
\_include (public include directory)
\_subdir2
| Public .h files (included with #include <subdir2/foo.h>)

adding custom library to netbeans for C++

I've recently started watching STanford's cs106B lectures on youtube, and I've downloaded their "Stanford C++ Libraries" that they've made. I've right-clicked my project, and added the whole folder (named "cs106lib-0.3.1") to the "Include directories" and "Include Headers" sections" but when I import one of the headers "vector.h" and create an object using it it says "unable to resolve identifier vector", and the compiler says the folder doesn't exist, although it's definitely on my desktop. Sorry, if this question has been asked then I can't find it, but I have been stuck looking for the past day.
Don't mix include directories and -headers.
Include directories: Adds directories where your header files are
Include headers: Adds single header files
Also make sure your paths are ok. Let's assume a structure like this:
cs106lib-0.3.1
|
+-- include
| |
| +-- Example1.h
| |
| +-- subdir/Example2.h
|
+-- ...
In this case you have add the directory cs106lib-0.3.1/include to include directories.
Now you can use it like this:
#include "Example1.h"
#include "subdir/Example2.h"
// ...
Also don't forget to add the binaries (if you have) to linker flags.
TIP: Use the code completion to see where you are; eg. type #include "../" <Ctrl+Space> to see files and directories available for to include.

Visual Studio + CMake's GLOB_RECURSE

I'm browsing a project which has a src directory similar to:
src/
|-- A.cpp
|-- dir/
|-- |-- B.h
|-- |-- B.cpp
In A.cpp they include B.h as such:
// In A.cpp:
#include "B.h"
In the visual studio 2010 solution generated with CMake this compiles just fine, and there exists no conflict. However, if I make a new project (not from CMake, but manually) and do the include as above, VS will not find B.h. I.e.:
// In A.cpp (non-CMake project version)
#include "B.h" // Error: File not found (VS does not search the
//sub-directory when name is surrounded by "").
#include "dir/B.h" // Works: sub-directory specified.
The CMake file uses GLOB_RECURSE (which I assume is the reason the above works, please correct me if I'm wrong), and simplified it looks something like this:
cmake_minimum_required (VERSION 2.6)
project (cmake_test)
file(GLOB_RECURSE lib_SRCS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp *.h)
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/dir
)
add_library(lib STATIC
${lib_SRCS}
)
target_link_libraries(lib)
I've been examining the difference of the solution generated by CMake and the one I manually created, but I can't seem to find it.
My question is, in other words, how I could accomplish having VS find my B.h without the relative path included as the solution generated with CMake displays, without actually using CMake (such as by some option or configuration in my Visual Studio Project).
The part that makes it work is
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/dir
)
Your dir/ directory is added to the include directories. In Visual Studio you need to go to project properties at C/C++->General->Additional Include Directories and add dir/.