C++ linking <limits> library with g++ [duplicate] - c++

This question already has answers here:
Include header files using command line option?
(3 answers)
Closed 4 years ago.
I included these libraries into my source file main.cpp:
#include <iostream>
#include <climits>
My entire code written in Visual Studio 2017, Windows.
I transferred my code file to Linux.
When I try to compile with g++ Item.cpp main.cpp -o main , it gives an error for this part:
items[size - 1]->points = std::numeric_limits<int>::min();
So errors below:
main.cpp: In function ‘void insert_p(Item**, int, int&)’:
main.cpp:287:33: error: ‘numeric_limits’ is not a member of ‘std’
items[size - 1]->points = std::numeric_limits<int>::min();
^~~~~~~~~~~~~~
main.cpp:287:48: error: expected primary-expression before ‘int’
items[size - 1]->points = std::numeric_limits<int>::min();
^~~
A correct solution would be just replacing #include <climits>
with #include <limits>.
Unfortunately, here is the challenge: Is it possible to solve this issue without changing my source code?
Is there any compiling trick to fix this?
I'm looking for a command like g++ Item.cpp main.cpp -o main -llimits to link <limits> library.
[EDIT]:
Solved my problem with command below:
g++ Item.cpp main.cpp -o main -include "limits"
Check this: https://stackoverflow.com/a/3387518/7977464
And as #user4581301 said:
It's far better to fix the code.
Thank you all.

An easy solution would be just replacing #include <climits> with #include <limits>.
That's not an "easy" solution, it's a "correct" solution. Your source code is wrong - fix it instead of using weird workarounds.
Is there any compiling trick to fix this?
You could try to redefine the token climits to limits, but it will probably not work as it will break code trying to use climits.

Related

Problems with compiling c++20

I'm trying to play around with some new c++20 features but I am not able to compile even some simple lines of code..
I am working on macOS and first tried to compile it in Xcode with c++2a language dialect which didn't work out and then tried to compile the code from the command line with g++ but I got similar errors.
Everything is up to date (Clang Vers 12, g++/gcc vers 10) and I know that there are still some issues with some features.
So here is an example:
import <iostream>;
#include <vector>
int main(){
std::vector<int> vec{1,2,3};
for (int e : vec) std::cout << e << std::endl;
}
compiled with:
g++ -std=c++2a -fmodules-ts test.cpp
Throws the following error:
header file <iostream> (aka '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream') cannot be imported because it is not known to be a header
So I assumed there is a problem with the header path from the toolchain which hasn't the latest updates so I tried changing the path to g++ main includes.
So I tried:
g++ -std=c++2a -fmodules-ts -I/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/ test.cpp
Which throwed this error:
test.cpp:1:8: error: header file <iostream> (aka '/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/iostream') cannot be imported because it is not known to be a header unit
import <iostream>;
^
In file included from test.cpp:2:
In file included from /usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/vector:60:
/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/bits/stl_algobase.h:59:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
^~~~~~~~~~~~~~~~~~
2 errors generated.
Can someone help with this?
I am aware that I can make this compile using #include - this was just as an example for working with new features - I had the same issue with e.g. import .
I am really struggling with getting anything sorted - which compiler is the best usecase for c++20 and how and where can I include/locate the standardlib which has c++20 support?
And how can I use everything together in Xcode?
gcc 10 does not support this yet. You can use gcc trunk or the gcc modules branch, and you need to add -fmodules-ts to the command line.

Compilation errors for C++17 <filesystem> on MinGW

I want to play around with the new filesystem library that's now apart of the C++17 standard, however I can't get things to compile.
Things I've already tried:
Updating MinGW to 8.2.0
Compiling with g++ -std=c++17 test.cpp -o test
Adding -lstdc++fs to the compilation (this does not work, I get the error c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++fs)
Using <filesystem> as well as <experimental\filesystem>
Here is my simple test code just to try and get things compiling:
#include <iostream>
#include <filesystem>
using namespace std;
int main(int argc, char* argv[]) {
return 0;
}
and compiling with g++ -std=c++17 test.cpp -o test
With this I get the error(s):
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\filesystem:37,
from test.cpp:2:
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
|| (__p.has_root_name() && __p.root_name() != root_name()))
~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
from test.cpp:1:
... many more errors ...
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:603:7: note: suggested alternative: 'string_view'
string_type __tmp;
^~~~~~~~~~~
string_view
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:604:45: error: '__tmp' was not declared in this scope
if (__str_codecvt_in(__first, __last, __tmp, __cvt))
Does anyone else have any suggestions? It seems like most people are solving this by adding -lstdc++fs to compilation, but like I said that doesn't work for me.
Thanks!
The issue is with the mingw and gcc/g++ 8 branch itself, not with the compiler flags or pre-processor directives. The bug is open here.
Try using stable mingw-w64-7.x releases with #include <experimental/filesystem> directive and -lstdc++fs -std=c++17 flags. This will work for now, or otherwise wait for v9.1.0.
On experimental channel you need to use std::experimental::filesystem instead of std::filesystem.
If you don't want to go with experimental features, switch to MSYS2. It has v10.2.0-6 of gcc available as of Jan 2021.

how to fix the error: ‘::max_align_t’?

I get the error
"/usr/include/c++/5/cstddef:51:11: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^"
So I should update the libraries because I find this solution:
"A workaround until libraries get updated is to include <cstddef> or <stddef.h> before any headers from that library."
I wrote some command on the Ubuntu terminal such as:
bash $ sudo apt-get install apt-file
bash $ sudo apt-file update
bash $ apt-file search stddef.h
Then still the error exist.
Thank you
In the .cpp file where this compile error occurs you need to add
#include <cstddef>
before any of the other headers, e.g.
main.cpp (broken)
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Try to compile that:
$ g++ -std=c++11 -o test main.cpp
main.cpp: In function ‘int main()’:
main.cpp:5:10: error: ‘::max_align_t’ has not been declared
using ::max_align_t;
^
Then fix it:
main.cpp (fixed)
#include <cstddef>
#include <cstdio>
int main()
{
using ::max_align_t;
puts("Hello World");
return 0;
}
Compile and run it:
$ g++ -std=c++11 -o test main.cpp
$ ./test
Hello World
I compiled some code with GNU C++ 4.9 on CentOS, and the issue was not solved by ensuring top position #include (or by the older header name stddef.h).
Weird enough, I searched all header files of the compiler libraries for the global definition of max_aling_t as declared in the offending using declaration... and found none! Could it be in some 'internal compiled header?
So I simply commented-out the "using ::max_align_t;" line in the standard header (not proud of doing this indeed) and it solved the problem... and code is running...
if anyone can explain what is the meaning/impact of this max_align_t ?
I also commented-out the using ::max_align_t; line in /usr/include/c++/4.9/cstddef, while, code is running, but I don't know if there are any consequences by doing this...

error: ‘memset’ was not declared in this scope

I am trying to compile the complete example 8 provided at the end of this page: http://www.physics.wisc.edu/~craigm/idl/cmpfit.html
but I am getting this error: error: ‘memset’ was not declared in this scope
I have been looking how to solve this error and I saw that some people solved it by adding #include <string.h> to the head of the code. I tried it but I am still getting the same error.
I am using gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) on Ubuntu 12.04 LTS
I am trying to compile with:
g++ -o example example.cpp -lmpfit -lm
If you use C you should include string.h
Otherwise, if you use C++ you should use cstring
C: #include <string.h>
C++: #include <cstring>
I updated gcc to gcc-4.9 and I could compile just adding "#include " to the head of the code. Then I tried with gcc-4.8 and gcc-4.7, and again I was able to compile the code with no problem. Perhaps my previous compiler (gcc-4.6) was not correctly installed?¿. I will keep working with the last version of gcc. Thank you all for your help.

How can I build glib at c++ - compile error when build .cpp

All
When I build cpp using g++ and glib, I meet compile error.
c and gcc version is OK.
I'm building at Ubuntu 10.XX.
I even built it at windows - c++.
How can I build glib at c++?
Thanks you.
#include <glib.h>
int main() {
g_printf("test");
return 0;
}
jongwon_kwak#kwak-com:~/worksapce_CXX/TESTGLib/src$ g++ -c -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include TESTGLib.cpp
TESTGLib.cpp: In function ‘int main()’:
TESTGLib.cpp:12:17: error: ‘g_printf’ was not declared in this scope
you are missing #include <glib/gprintf.h>
Just edit this and it will work
#include <glib/gprintf.h>