Input File format not recognized (c++) - 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.

Related

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.

Execute .sh file in ubuntu on windows

I have created a file, 1.sh, as
c++ -c file1.cpp
c++ file1.o -o file1
And I tried to execute it on bash Ubuntu on windows. It tells me
: No such file or directory
c++: fatal error: no input files
compilation terminated.
However, if I execute
c++ -c file1.cpp
c++ file1.o -o file1
directly, the file (file1.cpp) can be complied normally.
My question is, what is the reason .sh file does not work and how to fix it?
Actually, I would like to write this as comment but there is not this formatting available. (I will delete this answer when it becomes obsolete.)
I just tried this (with g++ on cygwin's bash):
$ g++ -c nothing.cc
g++: error: nothing.cc: No such file or directory
g++: fatal error: no input files
compilation terminated.
$
Of course, where is no file nothing.cc in my current working directory...
So, it might be that my guess (C++ is running in the wrong working directory when started from shell script) might be reasonable...
As I already suggested: insert a
echo "$PWD"
at the beginning of your shell script to be sure.
Update:
Out of curiosity, I tried this also (cygwin, bash again):
$ c++ -c nothing.cc
c++: error: nothing.cc: No such file or directory
c++: fatal error: no input files
compilation terminated.
$
Now, it looks very similar to the OP.
The only fact that's puzzling me: The error message of the OP looks like whether the compiler didn't get any input:
$ c++
c++: fatal error: no input files
compilation terminated.
$
but I believe that's not the case because in the OP it starts with
: No such file or directory.
Thus, it rather looks like there is missing some text...

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 :).

How to pass a plain text file as a command line argument

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.

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++.