Code:Blocks c++ precompiled header linker errors - c++

I've just transferred a vc++ project over to code::blocks on linux, using the g++ compiler, as that's the target platform, and it's refusing to compile.
I'm getting errors like:
g++: error: inc/BarcodeServer.h.gch/Debug_inc_BarcodeServer_h_gch: No such file or directory
Firstly, I don't want to use pre-compiled headers anyway, so I have no idea why it's looking for .gch files. I just want to use standard .h headers.
Secondly, the file names it's looking for seem bizzare, like it's looking for a directory inside a file, though this could just my unfamiliarity with g++.
I'm not really sure what steps I need to take in Code::Blocks to have it simply use the .h files to compile. I've looked around, and in the build options there's a "precompiled header strategy" but none of the options are just "don't use them"
Any help would be appreciated.

Related

How to add precompiled headers in CMake for visual studio generator

CMake 3.18; VS 2019;
I'm learning CMake and to check my results I'm trying to generate visual studio project.
My current problem is precompiled header option.
What I want to achieve is that VS project will have "/use" or "/create" precompiled headers option with correct file name.
In my project folder I already have ugpch.h & ugpch.cpp files, which stands for precompiled headers.
I've tryied to use target_precompile_headers(UGame PRIVATE Source/ugpch.h) instruction in my cmakelists, but that has no effect on my visual studio precompiled header option.
What is the correct way to do this?
Edit: I've updated my cmake to 3.18 version, but still no effect that I expected in my generated visual studio project.
Instead I found new folder "Precompile header files" with cmake_pch.cxx file.. It looks like target_precompile_headers did something totally different from what I expected
The command target_precompile_headers has been added in CMake 3.16.
You'll either have to upgrade your CMake distribution (The one shipped inside VS2019 is quite up to date) or install the latest from the installer.
Otherwise, you'll have to use cotire for precompiled header, but my recommendation would be to upgrade since it's usually quite easy to do, and you probably already have an up to date version on your computer shipped with visual studio.
Since you updated your question.
The command target_precompile_headers is not doing what you think. You don't provide your PCH there, you provide what header should be part of the generated PCH. If the file ugpch.h contains includes for headera.h, headerb.h and headerc.h, then your precompiled header command should look like this:
target_precompile_headers(UGame PRIVATE headera.h headerb.h headerc.h)
The actual precompiled header will be generated by CMake.
Of course, you can still use ugpch.h as your precompiled header. CMake will simply use it to generate its own. As you observed.
After that, remove manual inclusion of the header. It will be included automatically.
From the CMake documentation:
The list of header files is used to generate a header file named cmake_pch.h|xx which is used to generate the precompiled header file (.pch, .gch, .pchi) artifact. The cmake_pch.h|xx header file will be force included (-include for GCC, /FI for MSVC) to all source files, so sources do not need to have #include "pch.h".
Why does it work like that you ask? CMake support PUBLIC precompiled header. If you link to multiple target that each have public precompiled headers, then your target inherit all of them. Since compilers usually accept one precompiled header, CMake has to generate agglomerated headers for that to work.
Using an older version of cmake...
i've used the /Yc and /Yu compiler flag with visual studio.
It is not as universal as target_precompile_headers(UGame PRIVATE Source/ugpch.h) but should do the trick :)

Where do I put these header/c files or libraries that I just downloaded? (C++, MinGW)

I haven't used online libraries before this.
I just downloaded libzip from https://libzip.org/ (was looking for c++ file to unzip zip files)
I now have this nice folder that's called libzip-1.5.2.
Where do I put it? I'm using mingw to compile, and just emacs to write.
I've tried copy and pasting it into mingw's various bin folders, lib folders, include folders, etc. A great variety of places within the C://mingw root folder. None of them work.
No matter where I look, every single question glosses over this, just saying "just include zip.h" but never mentioning how to actually set it up.
I guess this could be generalized to: I have a bunch of .c and .h files from a library I downloaded for my compiler. Where do I put them so that I don't get:
"fatal error: header.h No such file or directory"
Again, I use MinGW/G++ to compile, and just Emacs for writing. I'm running Windows 10.
Put it somewhere with an simple pathname. You have to tell g++ where to find the libraries and the headers. Normally this is done with the -L and -I option. Maybe you have to compile the library first. There you have to look at the install instructions.

g++ ignoring header files

So I am trying to create a exec with g++. My command is as follows:
g++ -o project21 main.cpp tools.hpp file.hpp FileInfo.cpp file.cp tools.cpp
I get an error
ld: warning: ignoring file tools.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
ld: warning: ignoring file file.hpp, file was built for unsupported file format which is not the architecture being linked (x86_64)
The behavior here is that odd is that I can compile it in XCode and using the exec just fine it is just in the shell that I am having the problem.
You don't need to compile the hpp files. Include them in your .cpp files.
Even if you include the hpp on your project, Xcode won't compile them, unless you explicitly ask it to (my mac is at work, but if I recall click the project (root of the tree at the left), click a target and check the tab build phases, there is a list with the files to compile).
About the segmentation fault, if it were missing source files (i.e, you added code in the .hpp files that really should be on a .cpp file), then you would get a link error, and not a segmentation fault. There are other reasons to the segmentation fault, run on GDB and debug it (compile with -g to be able to debug). Here are some possibilities:
If you are building for a different platform, the problem may be some platform specific code.
Compiler incompatibility: assuming you were using Xcode with LLVM, you may have some code that works with LLVM but behave different with gcc. Very unlikely (by my experience with those compilers, I can say they are very compatible), but still possible.
Try deleting any *.gch files that may have been created by the Xcode build before trying your g++ command.
However, in general I'd try to get things working without pre-compiling the header files first, then add the pre-compiled header optimization if it's necessary (i.e., don't pass the .hpp files to the compiler explicitly until you've got everything else working and only if you really need to).

Including Libraries C++

How do I properly include libraries in C++? I'm used to doing the standard libraries in C++ and my own .h files.
I'm trying to include wxWidgets or GTK+ in code::blocks and/or netbeans C/C++ plugin. I've included ALL libraries but I constantly get errors such as file not found when it is explicitly in the include!
One error: test1.cpp:1:24: wx/msw/wx.rc: No such file or directory : Yes the .h file library is included; what am I missing?
Do I need to be importing other things as well? Is there a tutorial for this? Obviously my shoddy textbook hasn't prepared me for this.
Firstly, header files are not the same thing as libraries. A header is a C++ text file containing declarations of things, while a library is a container for compiled, binary code.
When you #include a header file, the compiler/IDE needs to know where to find it. There is typically an IDE setting which tells the compiler where to look, or you can do it from the command line, normally using the -I switch. It sounds to me as if you have not set up the path to search for header files on in your IDE.
This means that test.cpp included "wx/msw/wx.rc" but that file cannot be found by your compiler. How to fix this depends on your compiler, but you need to find where wxwidgets is installed and add that to your "Include Paths" so that your compiler knows where to search for it. You can also put a more complete path to it in the include directive.
If the files are present in the same directory as test1.cpp, then probably you use the wrong kind of include. #include <...> is usually used for code that resides outside of the directory that your project is in. #include "..." is for includes inside your project directory (and then if they aren't found there, search in the same places as #include <> would).
It is quite an old question and this didn't really answer it for me.
I reinstalled wxwidgets into the root directory as someone suggested that being in a directory with a space in the name may be part of the problem.
Then I went into project > build options > search directories and removed all entries pointing to original install.
And this fixed the problem

Dev-C++ include file paths FLTK(Fast Light Toolkit)

When I compile and run programs in Bloodshed I save everything into a a folder labeled C++ in my username folder. When I downloaded FLTK, extracted it to the C++ folder, then tried to run a program using header files from FLTK, it was unable to find the files. My guess is that when the compiler looks for the header files it's only looking in the C++ folder, and the FLTK header files are embedded in folders that are inside of the C++ folder.
I googled around for a way to somehow have file paths that include looks into when it looks for the specified header file, but I couldn't find anything. Does anyone with experience using Bloodshed know how to do this?
Most people here probably don't use DevC++, having been warned off it by people like me. DevC++ has lots of problems and is no longer being developed. You should consider
switching to Code::Blocks, which is better in just about every way.
If you have installed FLTK properly, you should now have a program called "fltk-config". That program needs to be in your PATH. You need to edit your project's settings so that the output of "fltk-config --cflags" is added to your list of compiler flags and so that the output of "fltk-config --ldflags" is added to your list of linker flags.