I have source files written in C programming using notepad++ and I am running them from command lines and later i need to link them inorder to generate the .exe file.
Here are the following commands I want to use while generating .exe file
gcc logc.c -o logc
gcc mainc.c -o mainc
gcc -o output logc.o mainc.o
But when i run the following command my compiler is returning with the following error status.
gcc logc.c -o logc
(x86)/mingw-w64/i686-8.1.0-win32-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o):crt0_c.c:(.text.startup+0x39): undefined reference to `WinMain#16'
when i run the following command to compile my mainc file
C:\Users\user\AppData\Local\Temp\ccskY3nf.o:mainc.c:(.text+0x31): undefined reference to `Log'
collect2.exe: error: ld returned 1 exit status
And here are my mainc.c and logc.c and logc.h files for your reference
logc.c file is here
#include <stdio.h>
#include "logc.h"
void InitLog()
{
Log("Initializing Log");
}
void Log(const char* message)
{
printf(" %s",message);
}
mainc.c file is here
#include <stdio.h>
#include <conio.h>
#include <stdbool.h>
#include "logc.h"
int main()
{
int x = 5;
bool comparisonResult = x == 5;
if(comparisonResult == 1)
Log("Hello World");
return 0;
}
and logc.h file is here
#ifndef _LOG_H
#define _LOG_H
void InitLog();
void Log(const char* message);
#endif
How can i compile individual source files and then link them and generate an executable file.
Thanks in advance.
You don't create object files, for that you need the -c argument:
gcc logc.c -c
gcc mainc.c -c
gcc -o output logc.o mainc.o
By default gcc will generate an executable file, not an object file. So when you compile logc.c, it tries to make an executable but it can't find the main function so it fails. Similarly with main.c, it tries to make an executable but can't find Log
You need to add the -c option to create object files:
gcc logc.c -c -o logc.o
gcc mainc.c -c -o mainc.o
Related
How to change the entry point of a C program compiled with gcc ?
Just like in the following code
#include<stdio.h>
int entry() //entry is the entry point instead of main
{
return 0;
}
It's a linker setting:
-Wl,-eentry
the -Wl,... thing passes arguments to the linker, and the linker takes a -e argument to set the entry function
You can modify your source code as:
#include<stdio.h>
const char my_interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
int entry() //entry is the entry point instead of main
{
exit(0);
}
The ".interp" section will let your program able to call external shared library.
The exit call will make your entry function to exit program instead of return.
Then build the program as a shared library which is executable:
$ gcc -shared -fPIC -e entry test_main.c -o test_main.so
$ ./test_main
If you are on a system that provides GNU Binutils (like Linux),
you can use the objcopy command
to make an arbitrary function the new entry point.
Suppose a file called program.c containing the entry function:
$ cat > program.c
#include <stdio.h>
int entry()
{
return 0;
}
^D
You first compile it using -c to generate a relocatable object file:
$ gcc -c program.c -o program.o
Then you redefine entry to be main:
$ objcopy --redefine-sym entry=main program.o
Now use gcc to compile the new object file:
$ gcc program.o -o program
NOTE: If your program already has a function called main, before step 2, you can perform a separate objcopy invocation:
objcopy --redefine-sym oldmain=main program.o
Minimal runnable example and notes on other answers
main.c
#include <stdio.h>
#include <stdlib.h>
int mymain(void) {
puts("hello");
exit(0);
}
compile and run:
gcc -nostartfiles -Wl,--entry=mymain -o main.out main.c
# or -Wl,-emymain
./main.out 1 2 3
The notes:
without -nostartfiles, the link fails with:
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
presumably because the glibc setup code that runs before main in _start normally calls main.
command line arguments are not setup for you, presumably because they would be setup by the glibc code that runs before main, so trying to use them prints undefined values. I haven't found a method that works for them.
Tested in Ubuntu 20.10.
I want to create DLL in C and use it in Golang.
I used this tutorial to generate dll :
helloWorld.h
#include<stdio.h>
void __stdcall __declspec(dllexport) hello();
helloWorld.c
#include<stdio.h>
#include "helloWorld.h"
__stdcall void hello()
{
printf("Hello World !!");
}
I used this in command Prompt to compile it
g++ -c helloWorld.c
g++ -shared -o helloWorld.dll helloWorld.o -Wl,--out-implib,libhelloWorld.a
I was able to use the generated dll in this C code , named as example.c :
#include<stdio.h>
#include "helloWorld.h"
int main()
{
hello();
}
and compile it using
g++ -c example.c
g++ -o example.exe example.o -L. -lhelloWorld
But while using the DLL in Golang , i am getting error , please help me
test.go
package main
/*
#cgo LDFLAGS: -L. -lhelloWorld
#include "helloWorld.h"
*/
import "C"
func main(){
C.hello()
}
Error :
# command-line-arguments
C:\Users\kumarmoh\AppData\Local\Temp\go-build544493490\b001\_x002.o: In function `_cgo_525f579e070a_Cfunc_hello':
/tmp/go-build/cgo-gcc-prolog:40: undefined reference to `hello'
collect2.exe: error: ld returned 1 exit status
error image
Other info :
go version go1.11 windows/amd64
64-bit OS , x64-based processor , Windows 10
I tried creating dll in Visual studios and it gave me error as explained here .
my folder/file structure:
testing
folder
head.h
main.c
main.c
#include "head.h"
int main(){
foo(3);
return 1;
}
head.h
void foo(int x){
x = 5;
}
in cmd I go to "testing" folder and type in:
gcc -c -lfolder main.c -o main.o
but that gets me an error:
main.c:1:18: fatal error: head.h: No such file or directory
#include "head.h"
From what I understand, flag -ldir_name specifies that compiler should look inside of that folder for any #include files.
Can you please help me undersand what I am doing wrong. I also tried getting preporcessor's output:
gcc -E -lfolder main.c -o main.i
again this should look for head.h in "folder", paste its content inside of main.c
I'm trying to debug claws-mail notification plugin, I have code like this:
#include "notification_indicator.h"
#include "notification_prefs.h"
#include "notification_core.h"
#include "folder.h"
#include "common/utils.h"
#include <messaging-menu.h>
#include <unity.h>
#define CLAWS_DESKTOP_FILE "claws-mail.desktop"
#include <stdio.h>
void main(void)
{
GList *cur_mb;
gint total_message_count;
total_message_count = 0;
/* check accounts for new/unread counts */
for(cur_mb = folder_get_list(); cur_mb; cur_mb = cur_mb->next) {
Folder *folder = cur_mb->data;
NotificationMsgCount count;
if(!folder->name) {
printf("Notification plugin: Warning: Ignoring unnamed mailbox in indicator applet\n");
continue;
}
gchar *id = folder->name;
notification_core_get_msg_count_of_foldername(folder->name, &count);
printf("%s: %d\n", folder->name, count.unread_msgs);
}
}
and I'm compiling it with this command:
gcc -I/home/kuba/Pobrane/claws-mail-3.13.2/src/
-I/usr/include/gtk-2.0/
-I/usr/include/cairo/
-I/usr/include/pango-1.0
-I/usr/lib/i386-linux-gnu/gtk-2.0/include/
-I/usr/include/gdk-pixbuf-2.0/
-I/usr/include/atk-1.0/
-I/home/kuba/Pobrane/claws-mail-3.13.2/src/common
-I/home/kuba/Pobrane/claws-mail-3.13.2/src/gtk
-I/usr/include/messaging-menu/
-I/usr/include/unity/unity/
-I/usr/include/dee-1.0/
-I/usr/include/libdbusmenu-glib-0.4/
-c `pkg-config --cflags glib-2.0` test.c
but gcc create object file test.o instead of a.out how can I create executable file? I'm running this on Xubuntu.
Remove the -c option from the commandline (which generates the object file instead of executable).
From man gcc:
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form
of an object file for each source file.
Examples:
To generate an object file (`.o' file):
gcc -c test.c
To generate an executable:
gcc test.c -o test
(if you omit the -o test, it'd generate a.out as executable by convention).
How can I include foo() function of foo.c in this small program (sorry for my noob question):
In my foo.h file:
/* foo.h */
#include <stdio.h>
#include <stdlib.h>
int foo(double largeur);
In foo.c:
/* foo.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int foo(double largeur)
{
printf("foo");
return 0;
}
And in main.c:
/* main.c */
#include <stdio.h>
#include <stdlib.h>
#include "foo.h"
int main(int argc, char *argv[])
{
printf("Avant...");
foo(2);
printf("Apres...");
return 0;
}
After compiling:
$ gcc -Wall -o main main.c
I get this error:
Undefined symbols: "_foo",
referenced from:
_main in ccerSyBF.o ld: symbol(s) not found collect2: ld
returned 1 exit status
Thanks for any help.
$ gcc -Wall -o main main.c foo.c
GCC doesn't know to look for foo.c if you don't tell it to :)
Creating a program in C requires two steps, compiling and linking. To just run the compiling part, use the -c option to gcc:
gcc -c main.c
This creates an object file, main.o (or main.obj on Windows). Similarly for gcc -c foo.c. You won't get the error message above at this stage. Then you link these two object files together. At this stage, the symbol foo is resolved. The reason you got the error message was because the linker couldn't find the symbol, because it was only looking at main.o and not foo.o. The linker is usually run from gcc, so to link your object files and create the final executable file main, use
gcc -o main main.o foo.o
You have to compile foo.c also because it is another module. Let me see how they do it in gcc:
$ gcc -Wall main.c foo.c -o main
You could also do this in your MakeFiles, like this:
APP_NAME = Foo
Foo_HEADERS = foo.h
Foo_FILES = main.c foo.c
If you're not so much familiar with MakeFiles i suggest you to take a look at Make Docs, but this is a simple example, APP_NAME sets the name of the compiled executable(in this case is Foo), Foo_HEADERS will set the headers used by your application, Foo_FILES you will set the source files of your applications, remember to put the APP_NAME(in this case Foo) at the beginning of _HEADERS and _FILES. I suggest you to use MakeFiles because they will organize you application build process and will be better for the end-user.