My code
#include <stdio.h>
#define FILENAME "m_6"
int main() {
float f;
FILE * file;
// read values
if ( ! (file = fopen(FILENAME,"r")))
return 1;
while ( fread( &f, sizeof(float), 1, file ))
printf("%e\n", f);
fclose( file );
}
When I compile with gcc gcc -c n1.c -o n1
and try to run it I got
bash: ./n1: cannot execute binary file: Exec format error
File m_6 and n1.c were executed on the same machine.
3.13.0-76-generic #120-Ubuntu SMP Mon Jan 18 15:59:10 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
How to solve this?
gcc -c compiles source files without linking.
Cancel -c from from command.
Related
I'm using a new mac with macOS Monterey and the M1 Max chip. I'd like fopen to default to the current directory when no path is given (this is what I'm used to and I think is the norm), but it's defaulting to my user folder. So, for example, when I compile the below code, the resultant app, regardless of where it is, will only open file.txt if that file is in my user folder (I get a segmentation fault if it's anywhere else). Similarly, if I have the code make a new file with FILE* fp = fopen("new file.txt", "w");, the new file will appear in my user folder.
I'm compiling with gcc (that I had downloaded via downloading the newest version of Eclipse) from Terminal with the command:
gcc -std=c99 main.c -o yo
When I build using Eclipse or leave out -std=c99 I have the same issue.
Also, for gcc --version I get:
InstalledDir:
nathanschmidt#nathans-MacBook-Pro ~ % gcc --version Configured with:
--prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/usr/include/c++/4.2.1
Apple clang version 13.0.0 (clang-1300.0.27.3) Target:
arm64-apple-darwin21.3.0 Thread model: posix InstalledDir:
/Library/Developer/CommandLineTools/usr/bin
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE* fp = fopen("file.txt", "r");
printf("%d\n", fp);
int x;
fscanf(fp, "%d", &x);
fclose(fp);
printf("%d\n", x);
return EXIT_SUCCESS;
}
I'm very first at learning C and my gcc can't find the files
this is the file "getting-started.c"
#include <stdlib.h>
#include <stdio.h>
int main(void) {
double A[5] = {
[0] = 9.0,
[1] = 2.9 ,
[4] = 3.E+25,
[3] = .00007,
};
for (size_t i = 0 ; i<5 ; ++i) {
printf("element %zu is %g, \tits square is %g\n",
i,
A[i],
A[i]*A[i]);
}
return EXIT_SUCCESS;
}
this code runs fine.
this is what I tried to do
gcc -std=c99 -Wall -lm -o getting-started getting-started.c
Here is my error
gcc: error: getting-started.c: No such file or directory
What should I do?
I'm working on WSL ubuntu 18.04
Point to your directory first where getting-started.c is placed.
For example if your file path is this:
C:\Users\Name\getting-started.c
then write in command line of your WSL:
cd /mnt/c/Users/Name
Then run
gcc -std=c99 -Wall -lm -o getting-started getting-started.c
Recently, I tried to create a library and to do that I created two files: file-parser.c and array.c with their corresponded .h file. So, in the file-parser I am using a function from the array that calls a length and when I compile (without linking it) parser-file.c the commands
gcc -c -o file-parser.o file-parser.c
gcc -c -o array.o array.c
ar r libutils.a array.o file-parser.o
don't tell me an error.
The problem is that when I compile the main.c file
gcc -o prova main.c -L. -lutils
gcc returns:
array/libutils.a(file-parser.o): in function "AnalizeRow":
file-parser.c:(.text+0x109): undefined reference to "length"
array/libutils.a(file-parser.o): in the function "GetWord":
file-parser.c:(.text+0x16a): undefined reference to "length"
collect2: error: ld returned 1 exit status
I'm not English, so I think the error message took to me by ld.
Can anyone explain where was my error?
Thanks.
A similar error may take place in anywhere in a development process!!!
Your question does not give a complete picture about current development process.
I see, after read your question, you has two files with source code - array.c and
file-parser.c. Also, you mentioned the static library libutils.a, but you did not indicate - how are you created it and what contains it for now.
I try to recreate your current position:
The file file-parser.c
#include <stdlib.h>
int
AnalizeRow(char row[])
{
size_t len = length(row);
return 0;
}
char *
GetWord(char row[])
{
size_t len = length(row);
return NULL;
}
The file file-parser.h
int AnalizeRow(char row[]);
char* GetWord(char row[]);
The file array.c
#include <string.h>
size_t
length(char row[])
{
return strlen(row);
}
The file array.h
size_t length(char row[]);
Create the empty static library libutils.a
$ ar cr libutils.a
$ ar t libutils.a (it empty)
Your code
$ gcc -c -o file-parser.o file-parser.c
$ gcc -c -o array.o array.c
$ ar r libutils.a array.o file-parser.o
Content the libutils.a
$ ar t libutils.a
array.o
file-parser.o
Build and run
$ gcc -o prova main.c -L. -lutils && ./prova
I have not errors at all..
Preliminary conclusions:
I have wrong development process
Your static library libutils.a is cause the problem
Your source code in files is cause the problem
Testing environment
$ gcc --version
gcc (Debian 4.9.2-10) 4.9.2
$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 8.7 (jessie)
Release: 8.7
Codename: jessie
$ uname -a
$ apt-cache policy binutils
binutils:
Installed: 2.25-5+deb8u1
Candidate: 2.25-5+deb8u1
Version table:
*** 2.25-5+deb8u1 0
500 http://ftp.ru.debian.org/debian/ jessie-proposed-updates/main amd64 Packages
100 /var/lib/dpkg/status
2.25-5 0
500 http://ftp.ru.debian.org/debian/ jessie/main amd64 Packages
500 http://httpredir.debian.org/debian/ jessie/main amd64 Packages
I am writing a user ls command code in C. When I compile this code with cc lss.c, an a.out file is created, but then using ./a.out to run, I get an error.
My lss.c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
char cmdline[100];
if ( argc > 2 )
{
printf(cmdline, "ls %s %s", argv[1], argv[2]);
system(cmdline);
}
return 0;
}
When I compile and run, this is what happens:
$ cd "/media/dilip/New Volume1/c"
$ cc lss.c
$ ./a.out
bash: ./a.out: Permission denied
$
What is the cause of this error?
I think, you are trying to run your program on an NTFS partition, different from the one on which Mint is installed. Try to compile the program in your ext4 partition and generate the a.out there. It should run.
Compile and Run it on the Volume where your linux is installed.
Since Macosx Lion fread does not read file with length > 2G (int size, 2'147'483'648 bytes).
It worked for years with macosx snow leopard.
I wrote a program to test it :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
FILE *fin = NULL, *fout = NULL;
char *ptr = NULL;
size_t len;
fpos_t flen;
if (!(fin = fopen(argv[1], "rb")))
{
printf("The input file: %s could not be opened\n", argv[1]);
return -1;
}
if ((fout = fopen(argv[2], "rb")))
{
printf("The output file %s already exist\n", argv[2]);
fclose(fin);
return -1;
}
if (!(fout = fopen(argv[2],"wb")))
{
printf("Cannot write on output file %s\n", argv[2]);
fclose(fin);
return -1;
}
fseek(fin, 0, SEEK_END);
fgetpos(fin, &flen);
len = flen;
printf("Input file length : %zd\n", len);
fseek(fin, 0, SEEK_SET);
if (!(ptr = malloc(len)))
{
printf("Canot allocate %zd bytes\n", len);
fclose(fin);
fclose(fout);
return -1;
}
if (fread(ptr, sizeof(char), len, fin) != len)
{
printf("Cannot read file\n");
fclose(fin);
fclose(fout);
free(ptr);
return -1;
}
fclose(fin);
if (fwrite(ptr, sizeof(char), len, fout) != len)
{
printf("Cannot write file\n");
fclose(fout);
free(ptr);
return -1;
}
free(ptr);
fclose(fout);
return 1;
}
just run :
./pgm inputfile outputfile
openssl sha inputfile
openssl sha outputfile
There is no error.
The length of the 2 files are the same.
The two fingerprints are not the same.
(The pointer is well allocated and write in the outputfile)
Its only with fread, not fwrite.
i don't understand the problem.
I just see this program (i don't know if apple use this one on Lion) and
r variable is defined as int.
http://www.opensource.apple.com/source/Libc/Libc-186/stdio.subproj/fread.c
Thanks for answers
Sounds like you're not compiling in 64 bit mode. Look for a command line argument or an option to whatever compiler you're using. To make sure you're compiling in the right mode, printf("%d\n", sizeof(int)); and see if it shows you what you expected.
Works fine for me on MacOS X Lion (10.7.2) with XCode 4.2. The executable is a 64-bit program. You should ensure yours is too.
$ make 2gb
/usr/bin/gcc -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
2gb.c:5: warning: unused parameter ‘argc’
$ file 2gb
2gb: Mach-O 64-bit executable x86_64
$ dd if=/dev/zero of=input bs=1m count=3072
./2g3072+0 records in
3072+0 records out
3221225472 bytes transferred in 42.940363 secs (75016261 bytes/sec)
$ ls -l input
./2gb -rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:48 input
$ ./2gb input output
Input file length : 3221225472
$ openssl sha input
SHA(input)= c93bf6713a90e34554311f0a9e43cfd1f153475a
$ openssl sha output
SHA(output)= c93bf6713a90e34554311f0a9e43cfd1f153475a
$ ls -l input output
-rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:48 input
-rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:49 output
$ rm input output
$ /usr/bin/gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$
And, when forced into 32-bit compilation:
$ rm 2gb
$ make CC='/usr/bin/gcc -m32' 2gb
/usr/bin/gcc -m32 -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
2gb.c:5: warning: unused parameter ‘argc’
$ dd if=/dev/zero of=input bs=1m count=3072
3072+0 records in
3072+0 records out
3221225472 bytes transferred in 38.326753 secs (84046397 bytes/sec)
$ ./2gb input output
Input file length : 0
$ ls -l input
-rw-r--r-- 1 jleffler staff 3221225472 Oct 29 00:57 input
$
printf("%d\n", sizeof(int));
gcc -Wall Typ.c -o Typ
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long unsigned int’
warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long
--> 4
Thank you Jonathan.
i am on 10.7.2 and xcode 4.2
gcc --version
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)
i do gcc TestFile.c -o TestFile.o
and even exactly as you did :
/usr/bin/gcc -g -std=c99 -Wall -Wextra 2gb.c -o 2gb
and i add -m64 after
and i have again 2 differents fingerprint.
xcode 3.6.2 was removed using sudo /Library/uninstall-devtools --mode=all
and xcode 4.2 install after.
i run the pgm on another user newly created (cause of $path maybe) and its the same.
i don't understand.
Apple team answer after reporting this bug
Hello Stéphane,
This is a follow up to Bug ID# 10376104. After further investigation it has been determined that this is a known issue, which is currently being investigated by engineering. This issue has been filed in our bug database under the original Bug ID# 6434977. The original bug number being used to track this duplicate issue can be found in the Related Problem section of your bug report's Problem Detail view.
Thank you for submitting this bug report. We truly appreciate your assistance in helping us discover and isolate bugs.
Best Regards,
Developer Bug Reporting Team
Apple Worldwide Developer Relations