Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
I am compiling a large project for several platforms using GCC and Clang. The issue I have is that I do all of the bug fixing and testing on one platform (Ubuntu 18.04), and even run static tools like cppcheck and clang-tidy to find bugs. As part of the bug fixing, I even try to compile with several compilers on Ubuntu to make sure that the code is ready to ship.
However, several times I have run across the problem where a developer on another system can't compile the update due to a simple missing include.
A recent example is where we introduced some new functionality which was heavily tested in GCC and Clang on Ubuntu. Then a dev on MacOS got some compiler errors which turned out to be due to a missing #include <array> in one file, and missing #include <sstream> in another. I mean, when you look at the offending files, they were indeed using arrays and stringstreams, so I get it. But I am just surprised that the static tools didn't catch those errors.
So how do I solve this problem? They definitely are programming errors, not compiler bugs since it was obvious that I should have included the files.
You are looking for include-what-you-use. From their docs:
"Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo.h should #include a .h file that exports the declaration of that symbol.
Compiling it yourself isn't trivial as the inner workings of this tool is tightly coupled to the Llvm internals. But you might be lucky to find a pre-built package in your distro. Still, once you get it running, it's not a silver bullet. The problem it tries to solve is hard, there might be false positives etc.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I have the Windows 10 OS, use VS Code to write my C++ code and use CMD to compile my programs. I don't really know which standard the compiler in my PC (MinGW, gcc version 6.3.0) uses, but I just want to endure that it uses the latest one like C++14 or 17. Unfortunately, I need to type in -std=c++17 every time I need to compile my program using that standard. How do I set the desired standard as default?
Unfortunately, I need to type in -std=c++17 every time I need to compile
This is why build scripts exist. There are many arguments that you want to pass to your compiler at some point:
Source files (you may have multiple translation units = .cpp files)
Include directories
Libraries to link
Optimization level
Warnings
C++ standard
Symbol defines
...and many more compiler flags...
In bigger projects you may also have multiple build targets (and thus compiler invocations) and you don't want to do all that by hand every time either.
As a simple solution, you could write a .bat script that invokes the compiler with the right arguments for you. However, there are tools that do a way better job at this, such as make (generally only found in the Linux world) or MSBuild (part of Visual Studio). Then there are also tools that generate these scripts for you, such as CMake and several IDEs with their own project configuration files.
I just want to endure that it uses the latest one like C++14 or 17. Unfortunately, I need to type in -std=c++17 every time
-std=c++17 is exactly how you ensure that you're using the C++ version you want (in this case C++17).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a single-file CPP app, built for Windows and is running in command line only.
I am trying to make it compilable also for Linux.
The only libraries the app is using are:
#include <iostream>
#include "setjmp.h"
#include "windows.h"
#include "time.h"
It also uses all kinds of unsigned integers (uint8, uint16, uint32, uint64).
Other than this there aren't any other Windows-specific APIs.
My question is how to convert the code so it will be compatible with Linux?
Is there an easy way to do this?
Do it the other way round. Code, probably using some cross-platform framework like Poco or Qt (which is also usable in non-GUI code, e.g. using QtCore without QtGui), or Boost, in some Windows independent way and compile it regularly on both OSes.
BTW setjmp.h is C++ unfriendly (messing and incompatible with C++ exceptions), even on Windows.
So remove
#include "setjmp.h" /// wrong in all C++ programs
#include "windows.h" //// specific to Windows
then fix the code (e.g. all compilation errors; but on Linux compile with all warnin & debug info, e.g. with g++ -Wall -Wextra -g), probably using some framework like the ones I mentioned.
Don't convert C++ code (from Windows to Linux) but do try hard to write portable C++ code, thanks to some well chosen framework.
Perhaps your application could be written in some pure portable C++11 (but then, no need to #include "windows.h"). Command-line utilities like wc, cat, grep (with a subset of all features) could probably be written in portable C++11 -and might not even require any additional framework.
PS. Without having your source code and without any idea about what that application is, it is impossible to help you more. Your first task is to understand that code precisely and what it is supposed to do on Windows. Perhaps rewriting it in clean C++11 (maybe with some additional framework....) is the quickest way to do it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Can someone explain about toolchain dependency on OS and platform architecture, for instance if I want to compile code for an arm architecture, should I look for platform architecture or for OS that platform is running on and then adapt toolchain to it?
Most compilers compile their code to the assembly language. The code they produce will most likely depend on various calls to the operating system (e.g. to allocate dynamic memory), and have a header defining properties of the file like the location of the code and data sections (e.g. ELF, PE). An assembler then compiles this assembly to object files, which are linked using the linker of this platform. All these tools produce code for a specific architecture and OS.
This does not mean that the compiler and linker cannot run on another type of system. The process of compiling code for another system is called cross-compiling. Even though this is less commonly used than compiling for the same platform as the compiler runs on, it is quite commonly used. A few examples of this are compiling OS kernels, which of course cannot rely on another OS, or compiling native code for Android (the android NDK contains a cross-compiler).
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I am currently working on a Raspberry Pi project that uses the OpenCV library, among others. The OpenCV library is quite large, and the build process for it is decently extensive. I found a script which downloads and installs the latest version of OpenCV, and following some suggestions from this question, I was able to build the library, and begin using the functions within OpenCV.
Considering the actual build process for OpenCV took considerably longer than building our project would, is it acceptable to just build the library once, as opposed to building the library each time we build our project?
While I realize this is probably personal preference, I am wondering how others handle situations similar to this.
As you probably already know, code that does not change does not need to be recompiled. This is true for executables and libraries alike.
A library is supposed to provide you addictional functionality in a neat, pre-packaged form. The difference between additional code you add to your project and a library is that the code included in a library is supposed to be in a stable state, so that once built the user will be able to use its features without any maintenance hassle; the APIs will be available and they will always work. You will be able to discard any implementation files, and just work with the header files - which provide you with the API within your code - and the library files, which contain the compiled implementation.
You are pretty much pre-compiling part of your program; a part that will also be able to be used in other projects, again without recompiling.
Note that C++ itself is an example of this: an implementation of the C++ standard library (for example libc++) is already included with your compiler, so that you are able to use the standard C++ headers without the need to recompile C++ whole every time you try a "Hello World!" program.
You can even extract libraries out of parts of your project that you feel are already completed and stable: this can allow you to reduce the time required to compile your project even though it becomes bigger. These reasons are part of why modularity is so strongly encouraged when programming.
TL; DR: Recompiling a library only once is not only acceptable, is most probably what you want to do.
It is normal to compile once and then only link the library. For that reason the compilers can detect whether there are changes in source files.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Are there tools to transform source code in C++ to the source code in C/C++, but with instantiated (unrolled) templates?
This is necessary for an unambiguous understanding, into the what code C++ templates converted.
May be it is present in the IDEs(MSVS, QtCreator, ...) or in the compilers(ICC, GCC, MSVC, Clang)?
This seems already answered on SO
Debugging template instantiations
link 2
link 3 (with a nice paper too)
How do you debug heavily templated code in c++?
The Idea/principle from Alexey Frunze to use the disassembled code is quite good, together with the use of simplified templates there is a pretty good chance to understand exactly what it does.
Edit 1
There are a few other possibilities on how to get an understanding of the things which the compiler had done
Use: gcc -S -O1 {yourcode.cpp} to get the assembly and use the tool c++filt (its a part of binutils to convert the disassembly to C-Code if you feel more comfortable with C-Code
Use: g++ -fdump-tree-original file.cpp to get some (pseudo) C++ code
Use the MSVC++ debugger with the breakpoint after the last instantiation and see all types and values which are the parameters of the instantiated template
Use: GCC XML for generating XML with instantiated templates (FAQ)
To know how the compiler instantiated and optimized the templates you can use Clang: -emit-llvm to get the LLVM IR, and use llvm-dis to convert it to text
CPP insights is a website of a LLVM based tool to see instantiations
You could work around the problem by placing a deliberate error inside the instantiation or its parameters, then you'd have the compiler (assuming decent versions: gcc 4.8, clang, etc) output something along the lines of: "error with template XXX instantiated with A=int, B=float, ..".