g++ ignoring header files - c++

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).

Related

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.

How to link large C++-Project in Eclipse

I am currently working on a very large C++-Project in my company. My job is to build a code generator for certain parts of the project. The Project is managed in Kinetis Design Studio 3.0.0 which is based on Eclipse. The Compiler used is GNU ARM C/C++ Cross Compiler 1.12.1
Now my problem is: As the number of files my generator created increased, suddenly an error message appeared:
arm-none-eabi-g++: error: ./00_Hmi/FontsHandler/SEGE_UI_11B.o: No such file or directory
make: *** [CDSB_AutoGen.elf] Error 1
The message pops up when the linker tries to link all the object files to the final binary. The file that is not found by the linker does definitely exist.
If I exclude some files from the build the error disappears (obviously I have to fix some includes and parts of the code that depend on those files). It doesn't seem to matter which files I exclude. I can include all the generated files and just exclude some others. I found here that linkers have a maximum number of object files that can be passed in the command line. I couldn't find that limit for GCC C++-Linker but I believe this could be the problem. The compiler creates around 160 Object files but I am not sure if all of them are used in that final link stage. I am not an expert with make or with how Eclipse handles these things. But IF my guess is correct: Is there any way to tell the linker to use all the Object files?
EDIT:
I renamed the folder that contains most of the files. The linker command got way shorter and now it works fine. I may have to either use shorter filenames or pack some of the .cpp files together.
You can also reorganise your project structure and chop sources into a bunch of static libraries linked together at the end of the building process. This will help to reduce the size of final linkage command.

c++ including cpp files

when I work on my IDE (visual studio or xCode) I never include cpp files (only h files) and everything compiles perfectly.
however, when I compile on the cmd/terminal (mostly on my raspberry pi) if I don't include the cpp files the compiler throws many errors because he didn't read the cpp files.
why is that? how does the ide knows to include the currect cpp files? and is there a way to find out if you are compiling through an ide or terminal?
something like:
#ifdef IDE_KEYWORD
#endif
The IDE will typically compile each source file (what you are describing as a "cpp file") separately, to produce an object file for each, and then link all the object files (and other libraries). Mechanically, the IDE does that by invoking the compiler separately for each source file listed in the "project file" and then linking.
When compiling from the command line, it is necessary to specify EVERY source file on the command line - the IDE isn't helping with that, so you need to do it manually.
One option is to compile every source file separately (specify each on a separate command line) and then another command to link all the object files together to produce the executable. In effect, this (more or less) imitates what an IDE does.
A second option is to list EVERY source file on a single command line - which will cause the compiler to compile each one sequentially and then (depending on what command line options you specify) link the object files together.
IDEs track what files are in the project/workspace and what ones are not. For instance, Visual Studio has a block in the vcxproj file which stores all the compilation units as such
<ClCompile Include="yourFile.cpp" />
having an ifdef to determine if you are using an IDE should be just a matter of adding IDE_KEYWORD to your preprocessor definitions inside your IDE. then if you compile outside of that IDE, the IDE_KEYWORD wont be defined.
If you are in Visual Studio you can
#ifdef _MSC_VER
// your code
#endif
If not I suppose there is no way.
The error may occur because you are not compiling all the .cpp files and/or are linking not all object files.
If using g++ g++ -c file.cpp -o file.o for every file to compile them. Then g++ file1.o file2.o fileN.o -o finalExecutable.exe for linking.
If this seems complicated look into Makefiles. They are awesome!

Dev-c++ compilation and linking problems

I've inherited a dll file and its source code and I need to rebuild it. The dll file exports functions that are needed in another application. It was originally made using an older version of dev-c++ (probably a few years back). When I try to build it, the compilation process goes fine and the object files are created, but I get an error during the linking process. Here's an excerpt from the output of the compile log.
g++.exe: error: unrecognized command line option '--no-export-all'
g++.exe: error: unrecognized command line option '--add-stdcall-alias'
I am looking for a solution to this problem, solution meaning being able to rebuild the dll. I do not have contact with the creator of the source code. Any suggestions, ideas or answers would be much appreciated. I am using dev-c++ 5.11 and using the TDM-GCC 4.9.2 32-bit compiler.

Code:Blocks c++ precompiled header linker errors

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.