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;
}
Related
I was trying out dynamic linking of libraries. This is a toy program I wrote:
// prog.c
#include<stdio.h>
int sum(int, int);
int diff(int, int);
int main(int argc, char* argv[]) {
int a = 10, b = 5;
printf("Sum: %d\n", sum(a, b));
printf("Diff: %d\n", diff(a, b));
}
// sum.c
int sum(int a, int b) {
return a + b;
}
// diff.c
int diff(int a, int b) {
return a - b;
}
Compiled using these commands:
$ gcc -fPIC -c sum.c diff.c
$ gcc -shared -o libdtest.so sum.o diff.o
$ gcc prog.c -L. -l dtest
When I tried to run this, it says ./a.out: error while loading shared libraries: libdtest.so: cannot open shared object file: No such file or directory. The file libdtest.so is still in my folder. What is the mistake here?
I used readelf to read a.out file. There, in the dynamic section, linked libraries were libdtest.so and libc.so.6 . But I couldn't find any reference to their full paths. How does program interpreter locate those files and load them?
The dynamic library loader have a fixed set of paths that it uses to search for libraries. If you have a custom library which isn't in that set then the library won't be found and you get an error.
Unfortunately the linker doesn't automatically add the path to the libraries it links with by default to the finished executable. Instead you must specify a special option to add the path: -rpath.
You can't use -rpath directly with the gcc front-end program, it's a linker-specific option. You need to use the -Wl option:
$ gcc prog.c -L. -l dtest -Wl,-rpath=$PWD
If the library is or will be installed in some directory other than $PWD, then you need to provide the actual (full) path to the installation directory.
Other possible solution includes adding the path to the set the dynamic loader uses (not recommended for uncommon paths), or setting the LD_LIBRARY_PATH environment variable (which is not recommended at all).
I am using Debian and i downloaded SDL_image.h succesfully. (sudo apt-get install libsdl2-image-dev)
I wrote a simple code to tell if it sees PNG images, but I'm getting an error.
Code:
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL_image.h>
int main(){
if (SDL_Init(SDL_INIT_VIDEO) < 0) printf("ERROR SDL_Init() - VIDEO");
if (IMG_Init(IMG_INIT_PNG) < 0) prinft("ERROR IMG_Init() - PNG");
char fileName[50] = "im.PNG";
SDL_Texture *image = IMG_Load(fileName);
if (image == NULL) printf("ERROR image == NULL");
SDL_FreeSurface(image);
return 0;
}
I compiled it on the command line as follows
gcc SDL_learnT.c -w -lSDL2 -o SDL_learnT
And i am getting Error = "fatal error: SDL_image.h No such file or directory"
I tried to do the following but the result did not change
#include <SDL2_image.h> or #include <SDL2/SDL_image.h>
Edit: It seems from your latest edit that you've [already] solved your problem, so the following may be moot.
Install the development package for SDL2_image [which it appears you've already done--sigh].
On fedora, this is:
sudo dnf install SDL2_image-devel
On ubuntu:
sudo apt install libsdl2-image-dev
Use pkg-config in the gcc lines (e.g.):
gcc -o program program.c `pkg-config --cflags --libs` SDL2_image
or sdl2-config:
gcc -o program program.c `sdl2-config --cflags --libs` -lSDL2_image
In any case, the correct include is:
#include <SDL2/SDL_image.h>
You should be able to do:
find -xdev /usr -name SDL_image.h
find -xdev /usr/local -name SDL_image.h
Or, some ls commands.
Then, compare against the pkg-config output.
A last resort ... I've had trouble in the past with SDL2 and ubuntu (bionic). Ultimately, I uninstalled the standard packages and rebuilt/reinstalled from the source packages.
OT:
IMG_Load returns a surface, not a texture:
SDL_Texture *image = IMG_Load(fileName);
should be
SDL_Surface *image = IMG_Load(fileName);
And here:
if (SDL_Init(SDL_INIT_VIDEO) < 0) printf("ERROR SDL_Init() - VIDEO");
It is not enough to inform about the error, you should exit (or at least skip all SDL functions), a better approach:
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
SDL_Log("SDL_Init: %s", SDL_GetError());
exit(EXIT_FAILURE);
}
SOLUTION
It should be <SDL2/SDL_image.h> not <SDL_image.h>
Compiling with gcc should be as follows (Command Line)
$gcc FILENAME.c -o OUTNAME -w -lSDL2 -lSDL2_image
I have a c script
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
int main(void)
{
const gsl_rng_type * T;
gsl_rng * r;
int i;
double a,b;
double num;
a=10;
b=7.2;
gsl_rng_env_setup();
T=gsl_rng_default;
r=gsl_rng_alloc (T);
for(i=0; i<10; i++)
{
num = gsl_ran_gamma(r,a,b);
printf("%.8f \n",num);
}
gsl_rng_free(r);
return 0;
}
Which I have successfully compiled on a linux machine. I want to use the gsl library for other applications on my mac. So I first installed gsl using homebrew which seemed to be successful. To make sure everything was working right I tried to compile and run this script as follows
[ACC-259-imac:GDSC Gene Expression Modeling jmannhei$ gcc -Wall gamma.c -o gamma.out -lm -lgsl -lgslcblas
which resulted in the following output
gamma.c:5:10: fatal error: 'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>
^
1 error generated.
Which is exactly how I compiled it on Linux so I am not sure what is awry as I have compile c scripts in the past using this format from terminal on a mac before. My guess is it is not linking properly but I am not sure what I need to do to fix it. Thanks
Find your header file directory with brew list gsl or locate gsl/gsl_rng.h then tell your compiler where the headers are. I would need the following, for example,
gcc -Wall -I/usr/local/Cellar/gsl/1.16/include/ gamma.c -o gamma.out -lm -lgsl -lgslcblas
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.
I came across this when I was compiling a simple program:
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
Display* display;
int main(){
display = XOpenDisplay("");
if (display == NULL) {
printf("Cannot connect\n");
exit (-1);
}
else{
printf("Success!\n");
XCloseDisplay(display);
}
}
FYI, I have xQuartz installed.
I compile this program with "g++ -o ex ex.cpp -L/usr/X11R6/lib -lX11" command.
You may need to add symbolic link to X11 folder by:
sudo ln -s /opt/X11/include/X11 /usr/local/include/X11
In my case, I had to make include directory under usr/local.
you need to compile with:
g++ -o ex ex.cpp -I/usr/X11R6/include -L/usr/X11R6/lib -lX11
the X11 headers are installed with xQuartz, but you need to reference them explicitly
If you install xQuartz it installs into /opt/X11, and /usr/X11 and /usr/X11R6 are symlinks to this location
This solution worked for me for ruby-1.9.3-p362 on Mavericks.
sudo ln -s /opt/X11/include/X11 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/