How to compile using LoopTools in C++ - c++

I am trying to use the following tool for C++ which is called LoopTools: http://www.feynarts.de/looptools/
It basically gives a numerical value for some integrals.
The manual says that in order to use it I must:
Like in the Fortran case, it saves a lot of typing to specify the location of LoopTools
once in an environment variable. For example, in the tcsh, use
setenv LT $HOME/LoopTools/(hosttype)
Then compile the programs that use LoopTools with the following command: $LT/bin/fcc -I$LT/include (source files) -L$LT/lib -looptools
I tried doing this with the example code they provide:
#include <iostream>
#include "clooptools.h"
int main() {
ltini();
cout << B0(1000., 50., 80.) << endl;
ltexi();
}
I saved this code as "example.cpp" and tried compiling it using what they said, namely:
g++ example.cpp $LT/bin/fcc -I$LT/include -L$LT/lib -looptools
But I get the following
.../LoopTools-2.16/x86_64-Linux/bin/fcc: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
What should I do?

To me, it works with the following changes (Ubuntu 20.04, x86_64, LoopTools-2.16).
Insert the following line,
using namespace::std;
to your example.cpp, before the use of cout/endl.
In $LT/bin/fcc, at line 19, remove the -stdlib=libstdc++ compiler flag from the cxx variable, which seems invalid for g++:
cxx="${REALCXX:-g++ -O3 -g -fomit-frame-pointer -ffast-math -fPIC -Wall -m64}"
In $LT/bin/fcc, at line 53, add .cpp as a C++ file suffix:
*.cc|*.cpp)
Otherwise, you need to rename your example file to example.cc.
As you noticed, the library is installed into $LT/lib64 on 64-bit Linux. Compile the program with
$LT/bin/fcc -I$LT/include example.cpp -L$LT/lib64 -looptools

Related

is there any way that I can add my own custom c/c++ header file to where the standard header functions come from so that I can use them at any time [duplicate]

I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c

VS code c++ debugger setup using GDB does not work

So here's my problem. I have downloaded mingw g++ by using msys according to the official vs code website
Here are my files:
Now, when I try to build I get this error:
> Executing task: g++ -std=c++14 -g -o myfile.exe myfile.cpp <
cc1plus.exe: fatal error: myfile.cpp: No such file or directory
compilation terminated.
The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command g++ -std=c++14 -g -o myfile.exe myfile.cpp" terminated with exit code: 1.
And if I do have a .exe file:
edit: I also started getting errors with
#include <iostream>
I would make this a comment if I could. What is the name of the file where you have written #include <iostream>?* I think if you change the name of that file to "myfile.cpp", you might stop getting that error. You will probably get a different error saying that "main() cannot be found" or something like that, but that's an improvement from your current spot.
*I see it's O3.cpp. Try changing that to myfile.cpp.

Unexpected result when initializing variable in C++ using curly braces

I'm using atom to practice C++ (I'm very new). I just learned to initialize variables like the following:
#include <iostream>
using namespace std;
int main() {
int myInt {};
return 0;
}
When I build and run the previous code in codelite I receive no errors. However, if I compile my atom file dailyPractice10.cpp using my MacBook terminal (zsh) I get the following error:
dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
^
;
1 error generated.
I'm using the following command to compile it on terminal:
g++ -o dailyPractice10 dailyPractice10.cpp (compiles)
./dailyPractice10 (runs program)
Does anyone have any feedback why this code runs in codelite but doesn't compile in terminal?
Because this feature is added from c++11.
if you will like to try below command.it will work.
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
The key to fixing this issue is to set the C++11 (or above) standards while building your code.
In the console tab of the IDE, the following output is generated before the error. Notice that no standard is being defined while building the code:
make all
Building file: ../1.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"1.d" -MT"1.o" -o "1.o" "../1.cpp"
We need to add the --std=c++1x flag to the g++ command. The following solution is for the ones using the Eclipse IDE and the MacOSX C++ compiler:
Right click on the project from the "Project Explorer".
Go to Properties > C/C++ Build > Settings.
Under the "Tool Settings" tab, find "GCC C++ Compiler" > "Miscellaneous"
In the "Other Flags" text box, edit the text such that it looks like:
-std=c++17 -c -fmessage-length=0
If you intend to use any other c++ standard, replace "c++17" with the standard of your choice ( eg. c++20).
Apply Changes.
Run Clean, and the Build again.
you should try this to compile the Code
g++ -std=c++20 -o dailyPractice10 dailyPractice10.cpp

Compiling an external library on Linux

Good Day Everyone,
N.B - This problem has been solved - I have provided my own solution in the answer section however the solution provided by Jonathan is much shorter. Nevertheless, this was the following question I originally posted:
I am basically trying to compile a serial library (for UART communication) on Linux however I am not really sure how to correctly compile (I have mentioned what I have done so far below), any suggestions would be highly valuable. I am using the serialib library - which is composed of 2 main files (serialib.h and serialib.cpp) , you may directly view the source code of these files here (scroll all the way to the bottom and view the files in new tabs): http://serialib.free.fr/html/classserialib.html
I transferred these files (serialib.h and serialib.cpp) to my BeagleBone Black micro-controller which is running Debian (Wheezy) , g++/gcc (Debian 4.6.3-14) 4.6.3. I wrote my own program (uart.cpp is my file name) to access the functions provided by this library, this is what I wrote:
#include <iostream>
#include "serialib.h"
#ifdef __linux__
#define DEVICE_PORT "/dev/ttyO1"
#endif
int main()
{
serialib LS;
return 0;
}
So as you can see I am trying to access the 'seriallib' class. serialib.h, serialib.cpp and uart.cpp are all in the home directory. I also manually added the iostream library in serialib.cpp as I did not see it being declared in the original source code.
Now I am really unsure of how to compile such external libraries but so far I tried the following steps:
g++ -c -Wall -Werror -fPIC serialib.c to convert to PIC which gives the following error:
distcc[3142] (dcc_parse_hosts) Warning: /home/debian/.distcc/zeroconf/hosts contained no hosts; can't distribute work
distcc[3142] (dcc_zeroconf_add_hosts) CRITICAL! failed to parse host file.
distcc[3142] (dcc_build_somewhere) Warning: failed to distribute, running locally instead
g++ serialib.cpp -L /home/debian/serialib.h which gives the following error:
/usr/lib/gcc/arm-linux-gnueabihf/4.6/../../../arm-linux-gnueabihf/crt1.o: In function _start':
(.text+0x30): undefined reference tomain'
collect2: ld returned 1 exit status
distcc[3210] ERROR: compile serialib.cpp on localhost failed
As of now I am still finding out how to compile this and if I manage to work this out then I'll post my solution here too. Once again any suggestion will be highly valuable. Thank you all :) .
g++ -c -Wall -Werror -fPIC serialib.c to convert to PIC which gives the following error:
The "error" is not an error, it's a warning, telling you that your distcc setup is broken, but that it compiled locally.
That command doesn't "convert to PIC", it compiles the file serialib.c and produces a compiled object file, serialib.o
g++ serialib.cpp -L /home/debian/serialib.h
This is just nonsense. It tries to build a program from serialib.cpp and use the directory /home/debian/serialib.h (which isn't a directory!) to find libraries.
You don't need to "compile a library" you can just compile both the source files and link them together into a program. Either:
g++ -c serialib.cpp
g++ -c uart.cpp
g++ serialib.o uart.o -o uart
Or all in one command:
g++ serialib.cpp uart.cpp -o uart
You should read An Introduction to GCC to understand the commands, not just enter bogus commands without understanding them.
I have found a solution to this problem, hope this helps for all the future readers with similar problems. I have my own source code uart.cpp (Given in the question) which I want to compile, the external library is serialib that contains two main files (serialib.h and serialib.cpp), you will want to replace the following commands with respect to the files you have
Step 1: Compiling with position independent code
g++ -c -Wall -Werror -fpic serialib.cpp
Step 2: Creating a shared library
g++ -shared -o libserialib.so serialib.o , here the library is libserialib.so.
Step 3: Linking your source code with library
g++ -L /home/debian -lserialib uart.cpp -o uart
g++ -L /home/debian -Wall -o test uart.cpp -lserialib
You may save the library at a different path and you may have a different name of course. Suppose you have a library called libabc.so at the directory /home/user/myDir then the commands will be like:
g++ -L /home/user/myDir -labc your_code.cpp -o your_code
g++ -L /home/user/myDir -Wall -o test your_code.cpp -labc
test is out own program, lserialib is actually looking for libserialib.so and not serialib.o as gcc/g++ assumes all libraries start with lib and end with .so or .a and you can see the same goes for labc as it will look for libabc.so thus it is important to make sure your library name begins with lib and ends with .so or .a
Step 4: Making library available at run time
Here we provide the path where the library is actually stored, I saved it in the directory /home/debian which is why my command looks like:
export LD_LIBRARY_PATH=/home/debian:$LD_LIBRARY_PATH
if your library is saved at /path/to/file then the command will look like:
export LD_LIBRARY_PATH=/path/to/file:$LD_LIBRARY_PATH
This is to help the loader find the shared library and to view this path: echo $LD_LIBRARY_PATH and to unset this: unset LD_LIBRARY_PATH
To execute the program type either ./test or ./uart and in case of any modification to the main source code (uart.cpp in this case) , simply repeat step 3. I found the following link very useful: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html . Thank you to all of you who took time to read this question and especially those who gave me suggestions. If anyone has more or better solutions, feel free to post them here to assist future readers :).

Error: ‘to_string’ is not a member of ‘std’ when compiling in command line with gcc 4.8.2, works in NetBeans?

I am doing tests for my code with GoogleTest (1.7.0). I must have the tests compiling in both NetBeans (8.0.2) and straight from command line.
NetBeans has GoogleTest integrated as suggested here: https://youtu.be/TS2CTf11k1U.
I have also build GoogleTest from the command line (in different place) with the instructions provided with the package.
The problematic line of code is (a and b are doubles):
std::string input = std::to_string(a) + " " + std::to_string(b);
which gives error: ‘to_string’ is not a member of ‘std’, when compiling with
g++ -isystem -std=c++0x ~/gtest-1.7.0/include -pthread ~/Task1/test_source.cpp libgtest.a -o test_module1_1
as instructed in GoogleTest documentation.
The strange thing here is that when using the "Test" from NetBeans (as seen on video), the tests compile and run correctly.
I am using Ubuntu 14.04 and I have already tried to update g++ (not to a higher version though), and I have also tried specifying the version with -std=c++11 instead of -std=c++0x.
In NetBeans there is this warning visible:
Library File /usr/include/c++/4.8/string
but there is an unresolved #include <stddef.h>
in included /usr/include/_G_config.h
I found out that this might be because multilib and I have also tried to fix that by checking that it is installed. And this doesn't really cause any warnings during compile so it might be just that the IDE is confused.
But conserning the real problem about compile errors:
Is there something wrong with the GoogleTest framework, the compiler or my code?
PS. Preferably don't suggest that I just don't use to_string as this system must be usable by students studying C++11 and really it shouldn't be that they can't use all the functionality that should be supported.
I think your problem is the order in which you're passing command line arguments to gcc
g++ -isystem -std=c++0x ~/gtest-1.7.0/include ...
^^^^^^^^^^^^^^^^^^^
The -isystem flag should be followed by a path that contains headers which you want gcc to treat as system headers. So the -std=c++0x flag is being incorrectly consumed by the -isystem flag. You probably want the command line to be
g++ -std=c++0x -isystem ~/gtest-1.7.0/include ...