error while invoking an online header file involving futures - c++

I have written a template function wait_for_all(vector) which blocks until every future in the vector is ready and then exits.
The problem and an implementation were originally given by Stroustrup in "The C++ Programming Language", 4th ed, pg 1243. However, I have changed the implementation, since Stroustrup's didn't handle exceptions thrown by a passed future. I have successfully tested my implementation: http://coliru.stacked-crooked.com/a/43bc327ac9f77f48. This solution combines the function and the tester in a single source file.
However, when I split the function and the tester into 2 separate files on coliru, I run into an error for the tester.
Header: http://coliru.stacked-crooked.com/a/5f3e11b783baf2e7 : It's OK
Tester: http://coliru.stacked-crooked.com/a/9badf42d85fc4e96 : Gives an error
The compilation output for the tester is:
ln -s /Archive2/5f/3e11b783baf2e7/main.cpp wait_for_all.h #link to the header file
clang++ -std=c++14 -stdlib=libc++ -O2 -Wall -Wextra -pedantic-errors main.cpp -o wait_for_all_test
./wait_for_all_test
/usr/bin/ld: /tmp/main-28d9f5.o: undefined reference to symbol 'pthread_setspecific##GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)
bash: line 11: ./wait_for_all_test: No such file or directory
What exactly is this error and how do I fix it?

Related

I can't run gcov on visual studio code on my cpp project; .gcda file not generating

I want to use gcov and I can't seem to get the .gcda file anywhere. I followed the official gnu guide (link) on how to use it but whenever I run g++ --coverage test.o it pops the quoted message as an error. I assume this is because the file is already created? Upon using g++ -coverage test.cpp -c it creates an .o and .gcno of the test file, not the .gcda one though:
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o): in function main': C:/M/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to WinMain'
collect2.exe: error: ld returned 1 exit status
. I have to mention that this also happens with the a.out command

How do i call External Code from Oct Files without Errors?

I want to run a Fortran program within Octave. I found a document, that has an example that i followed step by step. Here is the link. I compiled fortransub.f with the command gfortran -c fortransub.f and fortrandemo.cc with the command mkoctfile -c fortrandemo.cc. Now i have two header files. In the document it says :
Both the Fortran and C++ files need to be compiled in order for the example to work.
For me it is really a vague statement. Now to my problem. I don't know if i have to create an octfile. When i try to create one with the command mkoctfile fortrandemo.cc i get the following error:
c:/octave/octave~1.0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\goebl\AppData\Local\Temp/oct-A409yh.o: in function `Ffortrandemo(octav
e_value_list const&, int)':
C:\Users\goebl\Desktop\Test_1/fortrandemo.cc:23: undefined reference to `fortransub_'
collect2.exe: error: ld returned 1 exit status
warning: mkoctfile: building exited with failure status
When i try to run the command that is the next step in the document, i get the following error:
mkoctfile fortrandemo.cc fortransub.f
gfortran: fatal error: cannot specify '-o' with '-c', '-S' or '-E' with multiple files
compilation terminated.
What did i miss or did wrong? Can someone help me out? Thank you very much!

How to compile this C++ code on Linux with relevant header files?

I am trying to compile this C++ code in my Linux box using g++ but it fails with the following error:
enigma/Enigma# g++ -I . main.cpp -o main
In file included from machine.h:14:0,
from tests.h:13,
from main.cpp:10:
plug.h:13:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
#import "util.h"
^~~~~~
/tmp/ccxyoEC2.o: In function `main':
main.cpp:(.text+0x10): undefined reference to `test_machine_encode_decode()'
collect2: error: ld returned 1 exit status
The error indicates that the compiler cannot find the tests.h file present in the same folder. How can I compile and run this code?
I now understand that I needed to link the object files together, I did so using:
g++ -c *.cpp
g++ *.o -o enig
It still does not work though, the resulting binary executes with ./enig but is broken and does not function as intended:
Entire encoded message: TZQA
Decoding now...
Entire decoded message: AHOJ
Entire encoded message: HBIU
Decoding now...
Entire decoded message: AHOJ
Entire encoded message: ZSNE
Decoding now...
Entire decoded message: AHOJ
Entire encoded message: ICRH
It just keeps encoding and decoding those random texts as opposed to the functionality mentioned on the git page I shared above.
Anything I'm missing?
The error indicates that the compiler cannot find the tests.h file present in the same folder.
No, it doesn't. In fact, the compiler successfully compiled main.cpp.
The error indicates that the linker cannot find test_machine_encode_decode. This is hardly surprising, since test_machine_encode_decode is defined in test.cpp. You have to link the object files of main.cpp and test.cpp to get a complete executable.
If you look at the actual code, you'll see that main only calls the test_machine_encode_decode() Unit-test. You'll have to implement the functionality from the readme yourself or you search through the git history and try to find out, if the program actually worked in the past.

g++ encounters a fatal error linking to crt1.o in function __start

I have been trying to compile and link Qt5, which means I have been messing with some ldconfig and include path defaults that I don't completely understand. I will do my best to limit my question to a very specific one, as my priority is fixing my compiler.
I have used a command to try
`gcc -print-prog-name=cc1plus` -v
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include
End of search list.
There is a problematic line at the beginning, about ignoring a non-existent directory when searching for the header. Unfortunately, this is exactly where g++ thinks it's a good idea to look for crt1.o, which I assume is some sort of binary that morphs the main() function into an executable:
g++ -Isrc --std=c++11 -g -c -o src/tissuecell.o src/tissuecell.cpp
g++ -Isrc --std=c++11 -g -c -o src/analyze.o src/analyze.cpp
g++ -o TissueCells src/tissuecell.o src/analyze.o
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
My compiler has not done this until a few hours ago (previously, it would compile!), and what comes to mind in terms of "irreversible commands" that I typed was a single:
sudo ldconfig
Can anyone get me out of this terrible pit of linking and compilation problems, and remove this pesky line from my include path?
Some questions that you might also be able to answer that would make me very happy:
How do I view & edit the paths that my compiler searches for includes? (Not -I)
How do I view & edit the paths that my compiler searches for libraries? (Not -L, and ld should be able to find the libraries as it needs them)
What relation does ld have to this whole process, and is there a way to pass ld some flags through my g++ call (for example, -rpath)?
Oh gosh, this is embarrassing. My problem was that I did not fix my Makefile after giving up on the new build; the error was coming from a lack of main() function in my codebase.
The following command allowed me to test my compiler for internal errors:
echo "int main() { return 0; }" > /tmp/test.c; gcc -v /tmp/test.c
I pulled back to an old commit on my git, and got my code to compile, which served as a good double-check on my compiler's sanity.
First of all you can run gcc(g++) with "-v" option, for example:
echo "int main() { return 0; }" > /tmp/test.c
gcc -v /tmp/test.c
You find in ouput the things like:
--with-gxx-include-dir=/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4
plus
#include <...> search starts here:
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include-fixed
/usr/include
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../../x86_64-pc-linux-gnu/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/../../../:/lib/:/usr/lib/
and many other stuff.
The second variant you can use "strace -f" in front of your command, grep ouput with pattern open to find out where and what file is used.

File not recognized: File truncated GCC error

I'm trying to compile a simple "Hello World" program in Linux using Eclipse, but I always get this:
Building target: hello
Invoking: GCC C++ Linker
g++ -o "hello" ./src/hello.o
./src/hello.o: file not recognized: File truncated
collect2: ld returned 1 exit status
make: *** [hello] Error 1
**** Build Finished ****
Does anyone have an idea what the problem is?
Just remove the object file.
This error most likely appeared after the previous build was interrupted and object file was not generated completely.
Just as an info if someone comes around here.
Another problem (with the same error) could be, that you are using ccache for faster compilation. In this case, the corrupt *.o file is there as well. So for that reason you have to clean the cache with
ccache -C (note the upper case C!)
Wasted me hours the first time ;-)
I think
g++ -o "hello" ./src/hello.o
should be ./src/hello.(c | cpp | cc depending on your language type)