I used to develop Java project by Eclipse.
A Java project can contain many code files with the main function (the entry point), so we can run each code file which has a main function.
But now I want to develop C project by Eclipse CDT.
In a C project we can have only one main function. Can I have many code files with a main function and run each file just like I would in Java?
P.S.: I don't like to write Make Target for each file by main self
Javas option to have a main in every object is highly irritating and does not make sense to me.
I assume you want to train a bit in c and want to find a way to have all training lessons in one file. Here is an option that would do that for you in a crude way. This would not be reasonable to do as an application but you can use this to execute different options.
To use this you would call your program like 'progname 1'.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int ProgrammingLesson001(void);
int main(int argc, char * argv[])
{
int i;
int option;
int e = 0;
printf("%i\n", argc);
for(i = 0; i < argc; ++i)
{
printf("%s\n", argv[i]);
}
if(2 == argc)
{
option = atoi(argv[1]);
printf("Your Option was '%s' interpreted as %i\n", argv[1], option);
}
else
{
option = 0;
e |= 1;
}
switch(option)
{
case 0:
printf("zero is an error\n");
e |= 1;
break;
case 1:
e |= ProgrammingLesson001();
break;
default:
break;
}
if(0 != e)
{
printf("an error has occureed\n");
}
return e;
}
int ProgrammingLesson001(void)
{
printf("This could behave like a main for training purposes\n");
}
If you spent some time programming in c take a second look at makefiles.
You can create a makefile that will fit all your programs.
This reduces the actual work you need to put into this so much that maintaining the switch construct is harder than creating a new project.
Thank Clifford to fixed my quest and guys who replied
I have solved this problem by myself
Here is my solution:
#!/bin/bash
SourceFile=$1
Path="$(dirname "$SourceFile")"
ParentPath="$(dirname "$Path")"
OutputPath="$ParentPath/bin"
OutputFile="$OutputPath/a.out"
mkdir -p $OutputPath
rm -f $OutputFile
gcc -w $SourceFile -lm -o $OutputFile
$OutputFile
This bash's name is gcc.sh
In eclipse run -> external tools -> external tools configurations -> new a configuration
Location: /home/xxxxx/gcc.sh
Working Directory: (just let it be empty)
arguments: ${resource_loc}
Then you can run C file by your customize command
Related
I'm writing a package manager for the Termux terminal emulator on android using the APK format. The program is written in C and uses various arguments like 'sync', and 'remove'. However, the function I have written doesn't recognize the argument I have written for the name of the package to 'sync'. 'sync' is meant to download an apk from the fdroid repositories and open it using xdg-open (not yet implemented) using the name of the apk given in the arguments.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void syncapk(char * apkname);
int main(int argc, char **argv)
{
if(argc==1) {
printf("Displaying help screen.\n");
} else if(argc>=2) {
if(strncmp(argv[1], "sync\n", 4) == 0) {
syncapk(argv[2]);
} else if(strncmp(argv[1], "upgrade", 7) == 0) {
printf("Upgrading all packages!\n");
} else if(strncmp(argv[1], "remove", 6) == 0) {
printf("Removing package!\n");
}
}
return 0;
}
void syncapk(char * apkname) {
printf("Syncing package: %s!\n", apkname);
char * synccmd = "fdroidcl download %s", apkname;
system(synccmd);
}
GCC (my compiler) says that the argument (the name of the apk I wish to download from the repositories) is 'redeclared as a different symbol'. I am fairly new to writing programs in C so feel free to critique other things, not just the problem itself and whether I could take a different approach completely.
This doesn't do what you think it does:
char * synccmd = "fdroidcl download %s", apkname;
This is defining a variable of type char * named synccmd and a variable of type char named apkname. The latter conflicts with the parameter of the same name, hence the error.
If you want to build a formatted string, you need to use sprintf to do that:
char synccmd[100];
sprintf(synccmd, "fdroidcl download %s", apkname);
Consider the following C program:
#include <stdio.h>
const int OP_0 = 0;
const int OP_1 = 1;
const int OP_2 = 2;
int op_0(int x) {
return x + 2;
}
int op_1(int x) {
return x * 3 + 1;
}
int op_2(int x) {
return 2 * x * x - 10 * x + 5;
}
int compute(int op, int x) {
switch (op) {
case OP_0: return op_0(x);
case OP_1: return op_1(x);
case OP_2: return op_2(x);
}
return 0;
}
int main() {
int opcode;
int number;
printf("Enter the opcode: ");
scanf("%d", &opcode);
printf("Enter the number: ");
scanf("%d", &number);
printf("Result: %d\n", compute(opcode, number));
return 0;
}
It is a very simple program that lets the user select one of 3 operations to perform on an int input. To use this program, we can compile it with, for instance, gcc program.c -o program, and then run it with ./program. That's all obvious. Suppose, though, that we wanted to add another operation:
int op_3(int x) {
return 900 + x;
}
If we wanted to use this new operation, we'd need to recompile the entire program. Adding a new operation to this program has O(n) complexity, and is slow since it requires a complete recompilation.
My question is: is it possible, in C, to let this program add new native operations (without writing an interpreter)? In other words, is it possible to dynamically compile and add op_3 to the C program above, without having to recompile everything?
For illustration purposes, here is an example of what I have in mind:
int compute(int op, int x) {
// the first time it runs, would load `op_N.dll`
// the next time, would use the loaded version
// so, to add a new operation, we just compile
// it and add `op_N.dll` to this directory
Fun op = dynamic_load(op);
return op(x);
}
The only way I can think of is to compile a new dynamic library that is then opened by the program using dlopen()...
Another way, similar but perhaps more primitive, would be to compile the code into an object file and then load it into a mmaped region with execution permissions, jumping then to it using a function pointer.
To do this, compile the new function using gcc -c, clean the binary code from the headers with objcopy -O binary -j .text. Now in the program open() the resulting file and use the mmap() function to map this file in memory, giving as protections PROT_READ | PROT_EXEC. You'd look up the manuals for all this functions.
Note that I am assuming that you are on a unix system. I don't know much about Windows, but I imagine that something similar could be done with VirtualAlloc().
Well, what you are asking is the "Open Principle of SOLID". To do so, you need to have a dynamic dlsym obviously after dlopen. To have a dynamic dlsym you need to be able to read header files or a file with the proper function prototypes. Yes, you need to cast function pointers, but the typecast depends upon the types of your parameter list.
Edit:
Hard coding dlsym means you have to relink your import library to your executable every time you add a function to your shared object.
OR
You have two shared objects. One is the import library, and the other is the library that you want to add functionality. As David Wheeler said, "All problems of computer science could be solved with another level of indirection, except for the problem with too many layers of indirection.".
Complete noob-proof answer. As the other answers suggested, we can use dlopen and dlsym to dynamically load a shared library on C. First of all, let's create the lib. Save the following file as 0.c
int fn(int x) {
return x * 10;
}
Then, run the following command to create the shared lib:
clang -shared 0.c -o 0
Now, we must edit our compute function to load fn from 0.c dynamically and use it. First, we declare an fn : int -> int function pointer:
int (*fn)(int);
Then, we convert the operation to decimal (since we saved the shared lib as 0, no extension):
char file[256];
sprintf(file, "%d", 0);
Then, we load 0 dynamically:
void *handle = dlopen(file, RTLD_LAZY);
Then, we find fn on that lib, and assing to the fn function pointer:
*(void**)(&fn) = dlsym(LIB[op], "fn");
Then, we can just call it!
fn(5) // will return 50
Here is a complete example, that handles errors and stores the function pointers in a jump table (so we don't need to re-load the lib every time, obviously!):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
const int MAX_OPS = 256;
// Jump-table with available functions
int (*OP[MAX_OPS])(int);
// Array with shared libraries
void* LIB[MAX_OPS];
// Loads an operation dynamically
void load_op(int op) {
int (*fn)(int);
// Generates the file name
char file[256];
sprintf(file, "%d", op);
// Opens the dynamic lib
LIB[op] = dlopen(file, RTLD_LAZY);
// Handles error opening the lib
if (!LIB[op]) {
fprintf(stderr, "Couldn't load operation: %s\n", dlerror());
}
// Creates the function pointer
*(void**)(&fn) = dlsym(LIB[op], "fn");
// Handles error finding the function pointer
if (!fn) {
fprintf(stderr, "Couldn't load operation: %s\n", dlerror());
dlclose(LIB[op]);
}
// Adds to jump table
OP[op] = fn;
}
// Clears the dynlib objects
void close_ops() {
for (int op = 0; op < MAX_OPS; ++op) {
dlclose(LIB[op]);
}
}
// Applies the specified operation to an input
// Requires a shared object file with a name equivalent to the decimal
// representation of op to be loaded on the current directory
int compute(int op, int x) {
if (!OP[op]) {
load_op(op);
}
return OP[op](x);
}
int main() {
int opcode;
int number;
printf("Enter the opcode: ");
scanf("%d", &opcode);
printf("Enter the number: ");
scanf("%d", &number);
printf("Result: %d\n", compute(opcode, number));
return 0;
}
All the credit to the people who took their time to answer my question here and on #c on Libera.Chat. Thank you!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
// to generate numbers
void gen_data(int b[], int n)
{
int i;
for (i = 0; i < n; i++)
b[i] = rand() % 101;
}
// to display numbers
void disp_data(int b[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d \n", b[i]);
}
// insert at desired posn
void insert(int b[], int n, int elt, int pos)
{
int i;
for (i = n - 1; i >= pos; i--)
b[i + 1] = b[i];
b[pos] = elt;
}
// delete an elt at given position
void delete (int b[], int n, int pos)
{
int i;
for (i = pos + 1; i < n; i++)
b[i - 1] = b[i];
}
// driver code
int main()
{
int a[100], pos, n = 10, let;
int opt;
system("cls");
gen_data(a, n);
while (1)
{
printf("\n 1- Insert 2-Delete 3-Display 4-quit\n");
scanf("%d %d", &pos, &elt);
insert(a, n, elt, pos);
n++;
break;
case 2:
printf("enter position at which elt to be deleted: ");
scanf("%d", &pos);
delete (a, n, pos);
n--;
break;
case 3:
printf("the numbers are : \n");
disp_data(a, n);
break;
}
if (opt == 4)
break;
} // end while
}
Log:
Active debugger config: GDB/CDB debugger:Default
Building to ensure sources are up-to-date
Selecting target:
Debug
Adding source dir: C:\Users\Ranju\Desktop\you\lab pro\
Adding source dir: C:\Users\Ranju\Desktop\you\lab pro\
Adding file: C:\Users\Ranju\Desktop\you\lab pro\bin\Debug\lab pro.exe
Changing directory to: "C:/Users/Ranju/Desktop/you/lab pro/."
Set variable: PATH=.;C:\MinGW\bin;C:\MinGW;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C;C:\Users\Ranju\AppData\Local\Microsoft\WindowsApps;C:\Program Files\CodeBlocks\MinGW\bin
Starting debugger: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args "C:/Users/Ranju/Desktop/you/lab pro/bin/Debug/lab pro.exe"
done
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 8.1
Starting the debuggee failed: No executable specified, use `target exec'.
Debugger finished with status 0
In my case, removing space(s) from the project path resolved the problem. Maybe you should try to change path like this:
"C:/Users/Ranju/Desktop/you/labpro/bin/Debug/lab pro.exe"
I recently had the same issue when starting to use CodeBlocks on Windows10 (C::B version 20.03 in my case). The problem was that I also had MinGW installed long before I installed CodeBlocks and CodeBlocks took the gdb.exe from that path instead of taking it from the MinGW path that was installed within CodeBlocks.
The solution for me was to change the default executable path in Settings -> Debugger... -> GDB/CDB debugger -> Default to the gdb.exe that was installed when I installed CodeBlocks. So: <C::B_installation_path>\MinGW\bin\gdb.exe.
After that change, the problem was solved.
I don't know how relevant this may be, but I seem to have the same or similar problem. See the material at
How do you debug using 'Code::Blocks 20.03' (the "mingw" version)?
especially that after the sentence, I have hopefully made progress towards an answer..
I have now added a proper answer to the above question..
I asume that you have MinGW installed, in the directory C:\MinGW.
Perhaps, AT YOUR OWN RISK, you could try temporarily renaming the folder C:\MinGW to something else, and try running the debugger.
I had MinGW pre-installed, when I installed Code::Blocks 20.03 using codeblocks-20.03mingw-setup.exe ( the 8th April 2021 version , default installation accepted). I also had a problem debugging. There is now a fix for this, which seems quite general, applying even without a pre-installed MinGW, see the answer to the other question ( link provided above ).
The fix involves changing a debugger setting, but in your case, you seem to be trying to use the correct debugger, see the line that you give, which is shown just below
Starting debugger: C:\Program Files\CodeBlocks\MINGW\bin\gdb.exe -nx -fullname -quiet -args "C:/Users/Ranju/Desktop/you/lab pro/bin/Debug/lab pro.exe"
In the above, you appear to be trying to use a debugger, from the directory, C:\Program Files\Codeblocks\MinGW\bin, which seems O.K., rather than one from C:\MinGW\bin, which is where the debugger was, that I was trying to use, see the other question.
However, you may be using something other than the debugger from the directory C:\MinGW.
Whether or not you have MinGW installed in C:\MinGW, you may need to alter some Setting in Code::Blocks.
I also had this problem. I am currently running Code::Blocks 17.12 on Windows 10. In my case, I simply upgraded my compiler to Mingw64 bit. To fix: I changed an entry in the default.conf file located in c:\users\logonname\appdata\roaming\codeblocks
directory. After making a copy of the original file I changed the line corresponding to the old debugger, gdb32.exe to the new file gdb.exe in its new directory. That fixed the problem. This was the default.conf old line, before making my change: <![CDATA[C:\Program Files (x86)\CodeBlocks\MinGW\bin\gdb32.exe]]>.
I need to say that i am Newbie at C and i only wrote about 100-150 lines of code in C.
I need to read a .properties file with entries like the following:
Value1 = Hello
Value2 = Bye
I would like to get to the Values like this:
bla.getValue("Value1");
So i can work with it like this:
foo = bla.getValue("Value1");
bar = bla.getValue("Value2");
printf("%s - %s",foo,bar);
I don't need them for anything else, than printing them to the screen.
I found two questions here, which went into the right direction, but they couldn't help me in my task:
How to read configuration/properties file in C?
Properties file library for C (or C++)
I tried multiple of the answers of the thread above, but either way my compiler(minGW) doesn't like one of these lines:
using foo::bar;
or
using namespace foo;
When i try to compile my code, i get an error saying:
error: unknown type name 'using'
This is the code where i tried to implement the given solution of the thread above:
#include <windows.h>
#include <stdio.h>
#include <string.h>
using platformstl::properties_file;
int WINAPI WinMain(HINSTANCE a,HINSTANCE b,LPSTR c,int d)
{
char *tPath, *tWindow;
char *search = " ";
tWindow = strtok(c, search);
tPath = strtok(NULL, search);
properties_file properties("%s",tPath);
properties::value_type value1 = properties["Value1"];
properties::value_type value2 = properties["Value2"];
printf("Window: %s; Path: %s; %s %s",tWindow,tPath,value0,value1);
}
I use a WinMain, because the programm is about finding an open Window. I haven't included those parts of the code, because they are irrelevant for my question and worked completely fine. The strtok(); parts are working fine for me too. I need them, because the title of the window to find and the Path of the properties file are both given as commandline arguments:
programm.exe windowtitle path/to/properties/file
As i tried with other answers, which told me to load some libraries, i got to a point, where the needed libraries didn't contain the needed header files. Some of the libraries are even for c++, which i have a restriction on, so i can't use it.
I hope that made things a little clearer, as you may know that i am not used to ask questions here. :)
I solved my Problem with a big Workaround.
This is my final code:
if(vn != NULL){
for(i = 0; i < 1; i++){
if(fgets(temp, BUF, vn) == NULL){
printf("Line is empty");
return 2;
}
}
if(fgets(puffer, BUF, vn) == NULL){
printf("Line is empty");
return 2;
}
tVariable = strtok(puffer, find);
tValue = strtok(NULL, find);
}else {
printf("Unable to read File");
return 2;
}
I just read the second Line of the given file and cut it at the = sign.
I know, that i need to read the second line, because the Property i need is always found in the second line of the .properties file.
I now have my wanted Value in tValue, so i can use it to print it out with printf("%s", tValue).
First of all Sorry for my bad English. I am not native English.
I am going to write a program that list all available logical disk drives. Then ask the user to select a drive. then takes a file extension and searches that file type in given drive (including directories and sub-directories). Program should be able to run on windows xp and onward. It should be single stand alone application. I am not expert in C. I have some hands on C#. i have following questions in this regard.
1. Is there any IDE/Tool in which i can write C# like code that directly compiles to single stand alone application for windows?
2. Can you recommend some libs that i can use state forward for this purpose like using in C#? (I have seen dirent and studying it.)
I coppied some code that i am testing as a startup.
#include <windows.h>
#include <direct.h>
#include <stdio.h>
#include <tchar.h>
#include<conio.h>
#include<dirent.h>
//------------------- Get list of all fixed drives in char array. only drive letter is get. not path.
// add ":\" to build path.
char AllDrives[26];
DWORD GetAllDrives()
{
int AvlDrives=0;
DWORD WorkState=-1;
//TCHAR DrivePath[] = _T("A:\\"); //Orignal Type
//start getting for drive a:\ to onward.
char DrivePath[] = {"A:\\"};
//http://www.tenouk.com/cpluscodesnippet/getdrivetype.html
ULONG uDriveMask = _getdrives();
if (uDriveMask == 0)
{
WorkState=GetLastError();
printf("\r\nFailed to Get drives. Error Details : %lu", WorkState);
return WorkState;
}
else
{
WorkState=0xFF;
printf("The following logical drives are being used:\n");
while (uDriveMask) {
if (uDriveMask & 1)
{
UINT drvType=0;
drvType = GetDriveType(DrivePath);
if(drvType==3)
{
AllDrives[AvlDrives]= DrivePath[0];
AvlDrives++;
printf("\r\n%s",DrivePath);
}
}
++DrivePath[0]; //Scan to all scanable number of drives.
uDriveMask >>= 1;
}
}
return WorkState;
}
int main(int argc, char* argv[]) {
char DrivePath[]={"C:\\"};
char CurrentDrive[]={"C:\\"};
DWORD Drives=-1;
int d=0;
for( d=0; d<26; d++)
AllDrives[d]=' ';
Drives=GetAllDrives();
if(Drives >0)
{
int Length= sizeof(AllDrives);
for(int x=0; x<26; x++)
{
if(AllDrives[x]!=' ')
{
printf("\r\nFixed Drive : %c",AllDrives[x]);
}
}
}
getch();
}
You can use visual studio compiler to compile C and C++ in Windows, and it is available with its own IDE. When you install visual studio, it will install required libraries also to compile the C/C++ program. There are other IDEs and compilers available compatible with Windows like DevC++,CodeBlocks.