Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem] Pythagorean triplet #37

Open
ademclk opened this issue Aug 22, 2022 · 10 comments
Open

[Problem] Pythagorean triplet #37

ademclk opened this issue Aug 22, 2022 · 10 comments
Labels
challenges good first contribution Good for new contributors good first issue Good for newcomers

Comments

@ademclk
Copy link
Owner

ademclk commented Aug 22, 2022

Pythagorean triplet

Pythagorean triplet is a set of three natural numbers where a < b < c.

$a^2 + b^2 = c^2$

There is exactly one Pythagorean triplet for which $a + b + c = 1000$.

Find a, b and c and print these numbers out.

@ademclk ademclk added good first issue Good for newcomers challenges good first contribution Good for new contributors labels Aug 22, 2022
@ademclk ademclk linked a pull request Aug 23, 2022 that will close this issue
@ademclk ademclk removed a link to a pull request Aug 23, 2022
Legoeggolas added a commit to Legoeggolas/callofcode that referenced this issue Aug 28, 2022
Legoeggolas added a commit to Legoeggolas/callofcode that referenced this issue Aug 28, 2022
@onurbbr
Copy link
Contributor

onurbbr commented Sep 19, 2022

@Legoeggolas Your code doesn't work on g++ and clang. Update the project with your working code.

@Legoeggolas
Copy link
Contributor

@Legoeggolas Your code doesn't work on g++ and clang. Update the project with your working code.

Double checked, works just fine for me.
image
What problems are you facing with the code?
I have intentionally commented out an include directive in the header file, maybe that is the cause of your issue?

@onurbbr
Copy link
Contributor

onurbbr commented Sep 20, 2022

@Legoeggolas Your code doesn't work on g++ and clang. Update the project with your working code.

Double checked, works just fine for me. image What problems are you facing with the code? I have intentionally commented out an include directive in the header file, maybe that is the cause of your issue?

It's not my issue. I tried my linux machines. It's not working.

[onur@archlinux pythagorean_triplet]$ clang solution.cpp
/usr/bin/ld: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/Scrt1.o: in function _start': /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:103: undefined reference to main' clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

More detailed info:
[onur@archlinux pythagorean_triplet]$ clang solution.cpp -v
clang version 14.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/11.3.0
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/12.2.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.3.0
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
Found CUDA installation: /opt/cuda, version
"/usr/bin/clang-14" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name solution.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/mnt/SSD/Projects/GitHub/callofcode/challenges/C++/pythagorean_triplet -resource-dir /usr/lib/clang/14.0.6 -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0 -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/x86_64-pc-linux-gnu -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/backward -internal-isystem /usr/lib/clang/14.0.6/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/mnt/SSD/Projects/GitHub/callofcode/challenges/C++/pythagorean_triplet -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/solution-f8d086.o -x c++ solution.cpp
clang -cc1 version 14.0.6 based upon LLVM 14.0.6 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../x86_64-pc-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/x86_64-pc-linux-gnu
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/backward
/usr/lib/clang/14.0.6/include
/usr/local/include
/usr/include
End of search list.
"/usr/bin/ld" -pie --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/Scrt1.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/crti.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/crtbeginS.o -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/solution-f8d086.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/crtendS.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/crtn.o /usr/bin/ld: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/Scrt1.o: in function _start': /build/glibc/src/glibc/csu/../sysdeps/x86_64/start.S:103: undefined reference to main'
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

@Legoeggolas
Copy link
Contributor

@onurbbr
It is because my code is not supposed to run as-is.

You're supposed to either include the header (where a function is provided) and compile with that, or uncomment the main function (and necessary includes) in the header and source files.

@onurbbr
Copy link
Contributor

onurbbr commented Sep 25, 2022

@onurbbr It is because my code is not supposed to run as-is.

You're supposed to either include the header (where a function is provided) and compile with that, or uncomment the main function (and necessary includes) in the header and source files.

Activating main does not change the result. This time I'm getting a namespace error. When I enable Namespace, the linker cannot link the code you wrote.

@onurbbr
Copy link
Contributor

onurbbr commented Sep 25, 2022

[onur@archlinux pythagorean_triplet]$ clang solution.cpp
solution.cpp:37:10: error: no member named 'cout' in namespace 'std' std::cout << "The values are: "; ~~~~~^ solution.cpp:39:14: error: no member named 'cout' in namespace 'std' std::cout << val << " "; ~~~~~^ solution.cpp:42:10: error: no member named 'cout' in namespace 'std' std::cout << std::endl; ~~~~~^ solution.cpp:42:23: error: no member named 'endl' in namespace 'std' std::cout << std::endl; ~~~~~^ 4 errors generated.

When I enable std and iostream it gives another linker error.

[onur@archlinux pythagorean_triplet]$ clang solution.cpp
/usr/bin/ld: /tmp/solution-aaf7f3.o: in function main': solution.cpp:(.text+0x1bf): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x1cb): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: solution.cpp:(.text+0x22a): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x22f): undefined reference to std::ostream::operator<<(int)' /usr/bin/ld: solution.cpp:(.text+0x23e): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: solution.cpp:(.text+0x256): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x25d): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' /usr/bin/ld: solution.cpp:(.text+0x262): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /tmp/solution-aaf7f3.o: in function __cxx_global_var_init': solution.cpp:(.text.startup+0xc): undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: solution.cpp:(.text.startup+0x13): undefined reference to std::ios_base::Init::~Init()' clang-14: error: linker command failed with exit code 1 (use -v to see invocation)

Error details (--verbose)
[onur@archlinux pythagorean_triplet]$ clang solution.cpp -v
clang version 14.0.6 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/11.3.0 Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/12.2.0 Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.3.0 Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0 Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0 Candidate multilib: .;@m64 Candidate multilib: 32;@m32 Selected multilib: .;@m64 Found CUDA installation: /opt/cuda, version "/usr/bin/clang-14" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name solution.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/mnt/SSD/Projects/GitHub/callofcode/challenges/C++/pythagorean_triplet -resource-dir /usr/lib/clang/14.0.6 -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0 -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/x86_64-pc-linux-gnu -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/backward -internal-isystem /usr/lib/clang/14.0.6/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../x86_64-pc-linux-gnu/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/mnt/SSD/Projects/GitHub/callofcode/challenges/C++/pythagorean_triplet -ferror-limit 19 -stack-protector 2 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/solution-ed3d52.o -x c++ solution.cpp clang -cc1 version 14.0.6 based upon LLVM 14.0.6 default target x86_64-pc-linux-gnu ignoring nonexistent directory "/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../x86_64-pc-linux-gnu/include" ignoring nonexistent directory "/include" #include "..." search starts here: #include <...> search starts here: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0 /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/x86_64-pc-linux-gnu /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../include/c++/12.2.0/backward /usr/lib/clang/14.0.6/include /usr/local/include /usr/include End of search list. "/usr/bin/ld" -pie --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/Scrt1.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/crti.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/crtbeginS.o -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0 -L/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/solution-ed3d52.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/crtendS.o /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/12.2.0/../../../../lib64/crtn.o /usr/bin/ld: /tmp/solution-ed3d52.o: in function main': solution.cpp:(.text+0x1bf): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x1cb): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: solution.cpp:(.text+0x22a): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x22f): undefined reference to std::ostream::operator<<(int)' /usr/bin/ld: solution.cpp:(.text+0x23e): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: solution.cpp:(.text+0x256): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x25d): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' /usr/bin/ld: solution.cpp:(.text+0x262): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /tmp/solution-ed3d52.o: in function __cxx_global_var_init': solution.cpp:(.text.startup+0xc): undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: solution.cpp:(.text.startup+0x13): undefined reference to std::ios_base::Init::~Init()'

@onurbbr
Copy link
Contributor

onurbbr commented Sep 25, 2022

I tried ubuntu server and ubuntu docker image. The result is the same.

root@ubuntu:~# clang solution.cpp
/usr/bin/ld: /tmp/solution-6d7d40.o: in function main': solution.cpp:(.text+0x182): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x18e): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: solution.cpp:(.text+0x1ed): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x1f2): undefined reference to std::ostream::operator<<(int)' /usr/bin/ld: solution.cpp:(.text+0x201): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /usr/bin/ld: solution.cpp:(.text+0x219): undefined reference to std::cout' /usr/bin/ld: solution.cpp:(.text+0x220): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' /usr/bin/ld: solution.cpp:(.text+0x225): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /usr/bin/ld: /tmp/solution-6d7d40.o: in function __cxx_global_var_init': solution.cpp:(.text.startup+0xc): undefined reference to std::ios_base::Init::Init()' /usr/bin/ld: solution.cpp:(.text.startup+0x13): undefined reference to std::ios_base::Init::~Init()' clang: error: linker command failed with exit code 1 (use -v to see invocation)

@Legoeggolas
Copy link
Contributor

Legoeggolas commented Sep 25, 2022

@onurbbr
You are using clang instead of clang++
The former cannot compile C++ code unless you explicitly set some flags and linkers (even then I am unsure about that, given standards and all) and clang++ is the recommended way to compile C++ code. I'm sorry but I did not notice this earlier, since I am the most familiar with GCC and MSVC.

After you have added the appropriate include in the header file and uncommented the main function, it should compile properly.
Or, you can write your own main(), include the header, and compile that file with the given source file (the way I intended the program to be used). In hindsight, I should have used flags instead of commenting out deprecated code, which I will be sure to do in any other solutions I submit.

@onurbbr
Copy link
Contributor

onurbbr commented Sep 26, 2022

@onurbbr You are using clang instead of clang++ The former cannot compile C++ code unless you explicitly set some flags and linkers (even then I am unsure about that, given standards and all) and clang++ is the recommended way to compile C++ code. I'm sorry but I did not notice this earlier, since I am the most familiar with GCC and MSVC.

After you have added the appropriate include in the header file and uncommented the main function, it should compile properly. Or, you can write your own main(), include the header, and compile that file with the given source file (the way I intended the program to be used). In hindsight, I should have used flags instead of commenting out deprecated code, which I will be sure to do in any other solutions I submit.

Hmm. I tried with ubuntu vm and it worked. That's weird because my main machine is not compiling this code. Anyway problem is my main machine. It works on Raspberry Pi 4 too. Sorry for misunderstanding.

EDIT: My machine linked clang++ as clang. I don't know how this happened. A strange situation has arisen. I am sorry for this. Thanks for your time. Have a nice day, keep coding!

@Legoeggolas
Copy link
Contributor

@onurbbr
You're very welcome, and I'm glad that we could come to a resolution. Have a nice day too, and happy coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
challenges good first contribution Good for new contributors good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants