I want to compile static library in linux for windows. Following is the procedure I followed for compiling
Compile the source code of static library in linux using i586-mingw32msvc-cc -c static_lib.c -o static_lib.o
Created the static library in linux ar rv static_lib.a static_lib.o and ranlib static_lib.a
I created a sample program in eclipse on windows and linked this static library which is cross compiled in linux for windows. The compiler used at windows was mingw.
while compiling the program in windows eclipse, the compiler gives me the following error.
static_test\static_lib.a: file format not recognized; treating as linker script
\static_test\static_lib.a:1: syntax error
collect2: ld returned 1 exit status
Build error occurred, build is stopped
The Codes are as follows:
static_lib.c
#include <stdio.h>
void func(void)
{
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
printf("Hello\n");
}
sample_static.c
#include <stdio.h>
extern void func(void);
int main ()
{
printf ("Main function\n");
func();
}
kindly give me suggestions to compile and get it work.
Regards
Johnnie Alan
Try using the cross-compiler archiver instead of the native one, i.e. use i586-mingw32msvc-ar and i586-mingw32msvc-ranlib instead of ar and ranlib.
Or is that just a typo on the question?
Try i586-mingw32msvc-ar instead of plain ar. Typically ar in Linux will not support the PE format used for Windows programming. (Or you will have to instruct it to use PE format.)
Related
I'm trying to learn how to program in C.
I'm simultaneously learning C, C++, & Java. I have also coded in html and javascript successfully making rich websites.
I'm following video lessons on skillshare. Through VirtualBox I've set up a ubuntu installation, created lesson001.c, and attempted to compile it by entering "gcc lesson001.c"
The program:
#include <studio.h>
int main(){
printf("hello, world!\n");
return 0;
}
The error:
lesson001.c:1:10 fatal error: studio.h: no such file or directory.
The instructor is walking through the coding lesson on a pre-configured linux system, so he does have the same errors. It is frustrating that a comprehensive paid lesson set does not include critical setup parameters.
additional info: "gcc -v" returns about 20 lines of information on gcc 9.3.0, so I believe it is installed correctly.
Thank you
Change the #include <studio.h> declaration to #include <stdio.h>. A header file named studio.h does not exist in the standard library.
stdio stands for "standard input/output," and has nothing to do with "studio"! 😀
It should be stdio instead of studio.
stdio stands for Standard Input Output
Correctly formatted code :
#include <stdio.h>
int main(){
printf("hello, world!\n");
return 0;
}
Hi I want to build an application for windows in C, I program in linux and compile the code with gcc and mingw-w64.
I tried a simple program with output and input it works fine on windows.
But, I want to use sockets to connect to a server.
So I searched in google and found this tutorial http://beej.us/guide/bgnet/output/html/multipage/intro.html#audience
It says that in windows we need to include winsock and run some command
So I did:
#include <winsock.h>
int main(void)
{
WSADATA wsaData;
printf("Hello! This is a test prgoram.\n");
if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
fprintf(stderr, "WSAStartup failed.\n");
}
}
But when I try to compile it I get:
undefined reference to `__imp_WSAStartup'
collect2: error: ld returned 1 exit status
In command line all that I run was:
x86_64-w64-mingw32-gcc try.c -o a.exe
So what I should do?
If I compile in linux I don't need the winsock library?
How to fix this?
thanks
If you look at the WSAStartup Manual and scroll a bit down, you will find what library it is defined in: Ws2_32.lib
This is an "import library", a stub you need to link against for a windows program to use the respective DLLs. MinGW includes all standard windows platform import libraries. So you just need to link it, using -lws2_32.
I am new to Tcl scripting and would like to use C to embed Tcl codes.
This is the code that I have copied from a website to test the Tcl-C working.
test.c
#include <stdio.h>
#include <tcl.h>
void main ()
{
Tcl_Interp *myinterp;
char *action = "set a [expr 5 * 8]; puts $a";
int status;
printf ("Your Program will run ... \n");
myinterp = Tcl_CreateInterp();
status = Tcl_Eval(myinterp,action);
printf ("Your Program has completed\n");
getch();
}
I am using MinGW to compile this file.
I have copied the contents of the C:\Tcl\include folder into the C:\MinGW\include folder as well.
My gcc command for compiling :
gcc -o test.exe test.c
The error message shown :
C:\Users\user\AppData\Local\Temp\ccEHJKCb.o:tcl_connection_test.c:(.text+0x23): undefined reference to `_imp__Tcl_CreateInterp'
C:\Users\user\AppData\Local\Temp\ccEHJKCb.o:tcl_connection_test.c:(.text+0x3d): undefined reference to `_imp__Tcl_Eval'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\user\AppData\Local\Temp\ccEHJKCb.o: bad reloc address 0x20 in section `.eh_frame'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
I don't seem to have any libtcl file in the Tcl folder.
The Tcl version is ActiveTcl 8.5.15.0.297577.
Any help would be really appreciated.
Your example how to embed Tcl is outdated, and you are missing certain things in your link line (-ltcl85 for example). If you simply add -ltcl85 to your link line it should start to work.
It does not work in your case, because you installed the x64 (64-Bit version) of ActiveTcl, which provides x64 dlls, not 32-Bit ones. But the standard mingw gcc only works with 32-Bit libraries.
So to get this to work:
Download the 32-Bit ActiveTcl distribution
Compile your code with gcc -o test.exe test.c -Lc:/tcl/lib -Ic:/tcl/include -ltcl86
Adjust your path so the c:\tcl\bin\tcl86.dll is found in PATH, make also sure Tcl finds its libdir (set TCL_LIBRARY=c:\tcl\lib\tcl8.6)
run your program
But for more complex examples, you still need to initialise the library and a do some boilerplate code, so please call Tcl_FindExecutable(argv[0]); before the call to Tcl_CreateInterp() otherwise a few commands (e.g. clock might just not work as expected).
Have a look at http://www.tcl.tk/cgi-bin/tct/tip/66.html for some more details. Also have a look at the Tcl source distribution and the source for the tclsh shell.
You're very close to getting it right.
The Tcler's Wiki has a few examples, some of which are very confusing to be frank, but this one from this page is the best I've spotted recently. (The comments are mine.)
#include <stdlib.h>
#include <tcl.h>
int main (int argc, char *argv[]) {
Tcl_Interp *interp;
const char *script = "proc p1 a { puts $a }";
// Initialize the Tcl library; ***STRONGLY RECOMMENDED***
Tcl_FindExecutable(argv[0]);
// Create the interpreter, the execution context
interp = Tcl_CreateInterp();
// Initialise the interpreter
if (TCL_OK != Tcl_Init(interp)) {
fprintf(stderr, "Tcl_Init error: %s\n", Tcl_GetStringResult(interp));
exit(EXIT_FAILURE);
}
// Define a procedure
Tcl_Eval(interp, script);
fprintf(stderr, "res 1: %s\n", Tcl_GetStringResult(interp));
// Check if the procedure exists
Tcl_Eval(interp, "puts [info commands p*]");
fprintf(stderr, "res 2: %s\n", Tcl_GetStringResult(interp));
// Call the procedure
Tcl_Eval(interp, "p1 abc");
fprintf(stderr, "res 3: %s\n", Tcl_GetStringResult(interp));
// We could use Tcl_DeleteInterpreter to clean up here, but why bother?
return EXIT_SUCCESS;
}
What else were you missing? Simple. You forgot to tell the C compiler to use the Tcl library when building the executable; the compiler (or, more strictly, the linker) is in places a stupid piece of code. The exact option to use to get the linker to add the library in will depend on your system configuration, but is probably going to be -ltcl, -ltcl8.5 or -ltcl8.6; which it is depends on the filename and all sorts of things that we can't check exactly without being on your system. The names do fit a simple pattern though.
It's also possible that you might need to pass the -L option in to tell the linker about additional library locations. (There's an equivalent -I for telling the compiler where to find include files, so you don't have to copy everything into one gigantic unmanageable directory.)
The order of arguments can matter. Libraries should be listed after the source file:
gcc -o test.exe test.c -L/mingw/path/to/library/directory -ltcl86
(If you're using old, unsupported versions of Tcl — why would you do that?! — then the code above won't work because Tcl_Eval then took a writable string. But that was fixed many years ago and upgrading to a current version is the fix.)
I want to do some modifications to the glibc library. The first step is to be able to use a specific version when I compile a program. I'm under ubuntu 12.10 and my directories are :
/mydirectory/glibc-2.17 (where I have extracted the last version from the website)
/mydirectory/glibc-2.17-build (where I have executed the configure and make command)
/mydirectory/test/helloworld.c (where I have my helloworld program)
The helloworld.c is the following:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
char glibc[256] = "xxxx"; /* How to detect the glibc version here ? */
printf("hello, world\n");
printf("glibc version = %s\n", glibc);
return 0;
}
First how can I print the version of glibc ? (I think that there is a macro/constant in glibc for that).
Second, what command line should I use to compile my helloworld.c file to use the glibc that is in /mydirectory/glibc-2.17-build ?
Use -L pathname to explicitly specify a pathname to ld as Barmar has said in the comment.
It's suggested to use static linking -static or there might be problems during execution I think.
Actually my own solution to this problem would be: compile and link the source code as normal, and invoke with LD_PRELOAD set to your specified version of shared objects.
See http://linux.die.net/man/8/ld.so
I used to use a mac to write some C programs but it's not working now.
I have to use an old windows laptop for a while.
I installed codeblocks and tested a simple program using Pthreads. Unfortunately it didn't work.
pthread_create(&thrd1, NULL, thread_execute, (void *)t);
It keeps saying undefined reference to _imp__pthread_create
How can i fix it?
You've clearly got a version of pthreads for Windows. You just haven't included the .lib file in your linker settings. Do that and you should be golden.
You need to grab pthreads-win32 as pthreads is a Unix component not a Windows one.
If you are using MinGW you can MinGW installation manager and install packages that need to execute pthreads and openmp related tasks. Here is the procedure.
After opening the installation manager go to all packages and select the select packages named using mingw32-pthreads-w32 and select them for installation.
Then go to the installation -> Apply changes to install new packages. The you can use pthread.h and omp.h inside your c or c++ program without any problem.
This code works fine in an MSYS2 terminal on Windows.
All you need to do is to install gcc. (See further below.)
// hello.c
#include <omp.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *print_hello(void *thrd_nr) {
printf("Hello World. - It's me, thread #%ld\n", (long)thrd_nr);
pthread_exit(NULL);
}
int main(int argc, char *argv[]) {
printf(" Hello C code!\n");
const int NR_THRDS = omp_get_max_threads();
pthread_t threads[NR_THRDS];
for(int t=0;t<NR_THRDS;t++) {
printf("In main: creating thread %d\n", t);
pthread_create(&threads[t], NULL, print_hello, (void *)(long)t);
}
for(int t=0;t<NR_THRDS;t++) {
pthread_join(threads[t], NULL);
}
printf("After join: I am always last. Byebye!\n");
return EXIT_SUCCESS;
}
Compile and run as follows:
gcc -fopenmp -pthread hello.c && ./a.out # Linux
gcc -fopenmp -pthread hello.c && ./a.exe # MSYS2, Windows
As you can see, the only difference between Linux and MSYS2 on Windows
is the name of the executable binary. Everything else is identical.
I tend to think of MSYS2 as an emulated (Arch-)Linux terminal on
Windows.
To install gcc in MSYS2:
yes | pacman -Syu gcc
Expect output similar to:
Hello C code!
In main: creating thread 0
Hello World. - It's me, thread #0
In main: creating thread 1
Hello World. - It's me, thread #1
After join: I am always last. Bye-bye!
Reference
https://www.msys2.org/wiki/MSYS2-installation/