rastignac

joined 2 years ago
 

As much as neovim seems nice, I can't seem to find many resources to actually help start configuring neovim as a beginner. Help is very sparse, and the easiest thing seems to get a premade config, but then there are just too many features, and complexity gets in the way of understanding what's going on

 

I'll explain myself. I'm doing a small project using a few dependencies, so I have to add them when I call gcc. Everything else is really easy. I just have c files and header files. I find it really cumbersome to have to tell make what headers go with what c files when they have the same name. I don't see why we don't have a build system where you simply have to give a project folder with the name of source file with the main() function, give the name of the output executable, the external dependecies to be called with gcc, and that's it. Everything else can be automatically detected and linked apropriately, even with multiple folders inside the project folder. Does something like that exist that's simple to use, or is this doable in make?

[–] rastignac@programming.dev 1 points 1 year ago

Thank you for your answer :)

 

I just started to get into neovim today, and I decided to follow the first tutorial I came across (neovim from scratch) on youtube. The first thing that's done is to copy the repo into .config/nvim/. At startup nvim set up a bunch of stuff. is there anything that it sets up outside of the ./config/nvim file? Because I really dislike installing stuff on my computer that just stays even after deleting the installation folder.

 

Hello,

I've installed the library called Raylib (https://www.raylib.com/). I followed the instructions to install the dynamic library. However, when compiling using gcc, including the raylib.h header dynamically in the source code produces an undefined reference error for every function I use from that library. That means that the library functions weren't attached to the executable at runtime, so gcc didn't find the right .so file.

To actually produce an executable, I have to use a shell script, that for some reason needs raylib's source code path. But that goes against the point of installing a dynamic library, since the latter is used so that source code can be compiled without library functions, instead putting them somewhere else to be shared with other executables.

Can someone explain to me how gcc finds the .so files and if anyone has used raylib dou you understand what the shell script does?

5
submitted 2 years ago* (last edited 2 years ago) by rastignac@programming.dev to c/c_lang@programming.dev
 

Hi guys, I'm writing a program using the pbm "P1" format to create a random bitmap. The '1' char represents a black pixel and the '0' char represents a white pixel. All that has to be done to use this format is to basically "draw" the image in a file using this system. I just want to make a char matrix and randomly put black pixels until I hit a maximum percentage of black pixels which I don't want to exceed. Then I print that matrix to a file.

What I don't understand is that even if I decrease the value of PERCENTAGE, recompile, and execute the program, there is no noticeable difference, in fact I suspect it's the same image, although I can't be sure.

#include
#include

#define WIDTH 400 #define HEIGHT 250 #define TOTAL_PIXELS (WIDTH * HEIGHT) #define PERCENTAGE 0.01 #define BLACK_PIXEL '1' #define WHITE_PIXEL '0'

` int randomBrackets(int n){ return rand()/((RAND_MAX/n) + 1); }

int main(){
	
	char pbm_matrix[WIDTH][HEIGHT];
	for(int i = 0; i < HEIGHT; i++){
		for(int j = 0; j < WIDTH; j++){
			pbm_matrix[i][j] = WHITE_PIXEL;
		}
	}
	int total_black_pixels = 0;
	while((double)(total_black_pixels/TOTAL_PIXELS) < PERCENTAGE){
		int x = randomBrackets(WIDTH);
		int y = randomBrackets(HEIGHT);
		pbm_matrix[x][y] = BLACK_PIXEL;
		total_black_pixels++;
	}

	FILE* img_ref = fopen("bitmap1.pbm", "w");
	fprintf(img_ref, "P1 %d %d\n", WIDTH, HEIGHT); 
	for(int i = 0; i < HEIGHT; i++){
		for(int j = 0; j < WIDTH; j++){
			fputc(pbm_matrix[i][j], img_ref);
		}
		fputc('\n', img_ref);
	}
	fclose(img_ref);
	return 0;
}`

This to open the image file netpbm is needed, and maybe libnetpbm (I don't know, everything is preinstalled on Linux Mint). I'm using the exercise in Rouben Rostamian's book as reference.

EDIT: I'm sorry for the very poor formatting at the top of the code, I can't seem to get the macros to look good

[–] rastignac@programming.dev 2 points 2 years ago

Thanks a lot for the in depth explanation, this makes things a lot clearer. I'll try 'putchar()' and test a few more things and then come back to read this post again

[–] rastignac@programming.dev 2 points 2 years ago

Thank you, I realize that there's a whole other aspect I didn't even consider. I'm new to C and Linux so I'll follow your advice but it's making me want to learn more. Thanks again to both you and @adriator for your answers

[–] rastignac@programming.dev 1 points 2 years ago

Thanks a lot for your answer. This might be because of my shallow understanding of how a buffer works, but I don't understand why EOF isn't consumed by getchar() when the other bytes are consumed. Isn't a char just a number and EOF too (-1 I think)? I probably should try and understand buffers more

 
      ' char* strInput =(char*) malloc(sizeof(char));
        int ch;
        int letNum = 0;
        while((ch = getchar()) != EOF){
                letNum++;
                strInput = (char*)realloc(strInput,letNum*sizeof(char));
                *(strInput + letNum - 1) = ch;
        }
        printf("\n");
        printf("%s\n",strInput);
        free(strInput);`

This is the contents of main in a program I wrote that takes an undefined number of chars and prints the final string. I don't understand why but it only works if I press ctrl+D twice, and only once if I press enter before.

does anyone get what's going on? And how would you have written the program?