How to pass a plain text file as a command line argument - c++

I'm trying to pass a file to my c++ program via the command line. What I am trying is this:
g++ main.cpp quadTree.h Rectangle.h quadTree.cpp Rectangle.cpp obstacles_copy.txt -lX11 -lm -L/usr/X11R6/lib
I keep getting this error:
/usr/bin/ld:obstacles_copy.txt: file format not recognized;
treating as linker script
/usr/bin/ld:obstacles_copy.txt:1: syntax error
collect2: error: ld returned 1 exit status**
I open the file in my main() using
FILE *fp=fopen(argv[1],"r");
Any suggestions?

When you say "pass a file", what exactly do you mean?
In your example, you are trying to compile the text file into your binary, which is giving you problems.
You should instead pass in the file name at runtime as a command line argument or feed in the file to your stdin using cmd < file.
To pass in a file at runtime, you would call your program in bash with ./myprogram filename, then in your main, you can access the filename as argv[1], making sure to handle the case where argc == 1

Compile first
g++ main.cpp quadTree.h Rectangle.h quadTree.cpp Rectangle.cpp -lX11 -lm -L/usr/X11R6/lib
Then execute
./a.out obstacles_copy.txt
if you want to build the text file into the binary that can be done by using objcopy to convert it to a format that gcc will understand, but thus converted it will be a memory object (a big array of char) not a file. and file operations like fopen fgets etc will not work on it.

Related

How to compile using LoopTools in 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

Input File format not recognized (c++)

I'm writing some code for an assignment for a CS class at my university. This program is supposed to read in a .DAT file from the command line, assign the values in the file to an array, and then take that array and compute its average. The only issue I have is that I keep getting this error when I try to compile my code:
/usr/bin/ld:seven.dat: file format not recognized; treating as linker script
/usr/bin/ld:seven.dat:1: syntax error
collect2: error: ld returned 1 exit status
I've tried looking around on Google/StackOverflow a bit, but my programming knowledge is too limited to really understand what's going on, so I have no idea where my errors are. I'm compiling the program with this command:
g++ lab5.1.cpp seven.dat -Wall -o myprog
I can post some/all of the code if needed also.
The problem is that you are trying to compile the .dat file as part of the build process when creating your myprog executable. That is wrong. Build the executable first, then pass the .dat file to your program when you run it, eg:
g++ lab5.1.cpp -Wall -o myprog
myprog seven.dat
Inside your code, you will receive the seven.dat filename in your main() function's argv[] parameter. You can then open the .dat file and read its content as needed.

g++ error "file not found": It interprets command line argument string as a file

~/Documents$ g++ -std=c++11 -o Assignment1 filename.txt 10
It gets through assignment1, filename.txt, but when it gets to "10", it thinks it is a file name and states 10 can't be found, and it is an error. I have it declared as argv[2] in main.
Assignment1 is argv[0], filename.txt should be argv[1], and 10 should be argv[2].
If you need more information let me know.
You need to pass your two arguments to the executable that gets built from your compiler/linker. The way you described what you are doing seems to indicate you are trying to run your program at the same time you are trying to build it.
Try breaking it up into two command lines.
g++ -std=c++11 -o Assignment1 yourcode.cc
./Assignment1 filename.txt 10

how to successfully run a c++ file which uses ogdf libraries

I compiled the file (source.cpp) using the command
g++ -I/home/hrishikesh/Desktop/OGDF-snapshot/include -O2 source.cpp -o mytest -L/home/hrishikesh/Desktop/OGDF-snapshot/_release -lOGDF -lCOIN -pthread
and it got compiled successfully without giving any error message,resulting a file "mytest" in the same folder as the source.cpp in.
when I try to run the mytest file using command
./mytest
it shows this error message
./mytest: error while loading shared libraries: libOGDF.so: cannot open shared object file: No such file or directory
please help
You need to put libOGDF.so in the same folder than mytest
g++ -I /AbsolutePath/Desktop/OGDF/ main.cpp -L -l
/AbsolutePath/Desktop/OGDF/_release/libOGDF.a -lpthread
here main.cpp is the file.
copy and paste the line above in other text editors to get rid of the confusions between the usage of I and L and the spaces.
Be sure that you are writing the absolute path correct.
The a.out file will be generated in current directory. execute it by using:
./a.out

g++ comes up with errors when run with execlp, but not from the terminal?

Using fork() and execlp(), I'm trying to compile a cpp file to a .so. I'm running a copied g++-4.8 executable that's in my project's directory tree. When run from the terminal, everything goes smoothly and I end up with a working dynamic library, but when I make a child process and use execlp to do the same thing, I get an error:
g++-4.8: error: ###: No such file or directory
This is repeated 6 times with a few characters in the "filename" changed around a bit. To compare, this is what I write in the terminal:
$ g++/bin/g++-4.8 -fPIC -shared bob.cpp -o bob.so
...and this is the code that generates the error:
if (fork() == 0) {
execlp("g++/bin/g++-4.8", "g++/bin/g++-4.8", "-fPIC", "-shared", "bob.cpp", "-o", "-bob.so");
}
bob.cpp is in the working directory of the parent process, and the terminal code is executed from that same directory.
### is a typical sequence from the header of a binary (ELF) file. You forgot to NULL terminate your argument list to execlp(), so it read garbage and tried to pass it to g++.