Clang++ stdlib/cstdlib.h workaround

148 views
Skip to first unread message

Antonio Olivares

unread,
Jul 3, 2018, 2:39:49 PM7/3/18
to FreeBSD Questions
Dear kind folks,

I have a cpp file that used stdlib.h and it compiled and ran successfully.
I am trying it out with clang and it returns errors that it cannot find
stdlib.h, I remove the stdlib.h include statement and it still complains.

I made a copy of the c++ file and it compiles and runs correctly, but I
removed some stuff " --- " and only output the numbers. How do I deal with
stdlib.h requirements?

The program finds numbers that are triangular and square. It fails for
numbers that are bigger than 2147483647.

Thanks for ideas/suggestions/advice provided.

Best Regards,


Antonio
_______________________________________________
freebsd-...@freebsd.org mailing list
https://lists.freebsd.org/mailman/listinfo/freebsd-questions
To unsubscribe, send any mail to "freebsd-questi...@freebsd.org"

Shane Ambler

unread,
Jul 4, 2018, 12:51:08 AM7/4/18
to Antonio Olivares, FreeBSD Questions
On 04/07/2018 04:08, Antonio Olivares wrote:
> Dear kind folks,
>
> I have a cpp file that used stdlib.h and it compiled and ran successfully.
> I am trying it out with clang and it returns errors that it cannot find
> stdlib.h, I remove the stdlib.h include statement and it still complains.

You can add -I<path> as an argument that tells clang where to search for
header files. -L<path> does the same for libraries. Even when you remove
it another included file may still want it.

clang++ -I/usr/include -L/usr/lib -o myapp myfile.cpp

> I made a copy of the c++ file and it compiles and runs correctly, but I
> removed some stuff " --- " and only output the numbers. How do I deal with
> stdlib.h requirements?
>
> The program finds numbers that are triangular and square. It fails for
> numbers that are bigger than 2147483647.

Does that number have any significance to you? It is the largest number
that can be held by a 32bit signed integer. You can use an unsigned int
to get twice that or you can use a long long to get a 64bit integer.

Also related is that the math library functions have several variations
that take different size arguments.

--
FreeBSD - the place to B...Software Developing

Shane Ambler

Antonio Olivares

unread,
Jul 4, 2018, 8:32:17 AM7/4/18
to Shane Ambler, FreeBSD Questions
Shane,

Thanks for your input. For the moment I have removed the header file

#include <stdlib.c>

From the c++ program. In a for loop part, I had a set of statements to
output the values where the number was a triangular and square at the same
time in two lines. This was where the program was failing to compile and I
was concluding incorrectly that it had to do with the stdlib.h or cstdlib.h
file.

#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int i;
int n;
int k;

cout << "This program searches for numbers that\n";
cout << "are both square and triangular\n";

cout << "Up to what number do you want to search?\n\n";
cout << "Enter number: ";
cin >> n;
for (i=1; i<n; i++)
{
k = (i*(i+1))/2;
if (sqrt(k) - int(sqrt(k)) == 0)
cout << "\n" << k << " is a triangular number when i = " << i << "." <<
endl;
}
cout << "\n\n";
return 0;
}

About the big number, it would be nice to get it working for bigger
numbers, but I would need to run it on amd64 freebsd machine at work.
Maybe at some other time we can revisit this issue. Thank you for your
assistance.

When running the program if I enter a number like 10,000,000 I get bad
output. If I stop at 1,000,000 I get correct output.

https://oeis.org/search?q=0%2C1%2C36%2C1225%2C41616%2C&language=english&go=Search


Best Regards,


Antonio

Erich Dollansky

unread,
Jul 4, 2018, 11:25:41 AM7/4/18
to Antonio Olivares, FreeBSD Questions, Shane Ambler
Hi,

On Wed, 4 Jul 2018 07:30:54 -0500
Antonio Olivares <olivar...@gmail.com> wrote:

> On Tuesday, July 3, 2018, Shane Ambler <Fre...@shaneware.biz> wrote:
> > On 04/07/2018 04:08, Antonio Olivares wrote:
> >> Dear kind folks,
> >>
> Thanks for your input. For the moment I have removed the header file
>
> #include <stdlib.c>

is this just a typing error or is this the cause?

Erich

Antonio Olivares

unread,
Jul 4, 2018, 1:27:28 PM7/4/18
to Erich Dollansky, FreeBSD Questions, Shane Ambler
On Wednesday, July 4, 2018, Erich Dollansky <freebsd....@sumeritec.com>
wrote:
> Hi,
>
> On Wed, 4 Jul 2018 07:30:54 -0500
> Antonio Olivares <olivar...@gmail.com> wrote:
>
>> On Tuesday, July 3, 2018, Shane Ambler <Fre...@shaneware.biz> wrote:
>> > On 04/07/2018 04:08, Antonio Olivares wrote:
>> >> Dear kind folks,
>> >>
>> Thanks for your input. For the moment I have removed the header file
>>
>> #include <stdlib.c>
>
> is this just a typing error or is this the cause?
>
> Erich
>

Typing error!
It should be
``#include <stdlib.h>''
Or
``#include <cstdlib.h>''
And some variations omit the ".h" part. I had a cout statement in two
lines and I always got error message. Once I joined the statements to a
single line the program compiled successfully.

Best Regards,


Antonio

Shane Ambler

unread,
Jul 5, 2018, 2:42:39 AM7/5/18
to Antonio Olivares, FreeBSD Questions
On 04/07/2018 22:00, Antonio Olivares wrote:

> About the big number, it would be nice to get it working for bigger
> numbers, but I would need to run it on amd64 freebsd machine at work.
> Maybe at some other time we can revisit this issue. Thank you for your
> assistance.

You don't need a 64bit machine to use 64bit ints but you do need to use
a recent C/C++ standard, long long was introduced in C99 and C++11. That
is, if you have an issue with long long add -std=c++11 to your CXXFLAGS.

https://en.wikipedia.org/wiki/Integer_(computer_science)#Long_long

There is also a long double

https://en.wikipedia.org/wiki/Long_double

Try it -

#include <stdio.h>

int main()
{
printf("sizeof long long - %lu\n",sizeof(long long));
printf("sizeof long double - %lu\n",sizeof(long double));
return 0;
}


--
FreeBSD - the place to B...Software Developing

Shane Ambler

Reply all
Reply to author
Forward
0 new messages