What? C language is cross-platform? When we first learned the C language, we all know that C is not cross-platform. Where can I start with the cross-platform nature of C? Haha, you can hear Jeremiah. The cross-platform nature discussed in this blog is narrow. It mainly discusses the two most popular operating systems, Windows and Linux. Specifically, is a cross-VC and gcc compiler. This is just a brief discussion, and we will not study it too deeply. The main content is as follows:
1. cross-platform C Language
2. MinGW and Cygwin
3. Create dynamic and static libraries and call methods using GCC
4. The static/dynamic libraries created by VC ++ are replaced with those created by GCC.
1. cross-platform C Language
Everyone knows that Java is a cross-platform language, mainly because different JRE and Java runtime environments are installed on different systems. In this way, the same code is on different operating systems, the running effect is the same. In fact, the so-called cross-platform nature of Jeremiah is similar to that of Java. It mainly refers to the cross-platform nature of the GCC compiler. People who have read Jeremiah's previous blogs should know that Jeremiah is mainly engaged in the open-source project VLC. By the way, I have studied FFmpeg and Live555. These projects are actually developed based on Linux, however, Windows mainly relies on two environments: MinGW and Cygwin. Similarly, if we develop based on Linux and want to run it in Windows, we can use MinGW or Cygwin to okay. Jeremiah will be mainly engaged in C Programming Development in Linux in the future.
2. MinGW and Cygwin
Google "MinGW Cygwin/Gcc" will show many articles about MinGW and Cygwin. This article mainly introduces the similarities and differences between the MinGW and Cygwin Jeremiah Linux simulation environments. Let's take a look at idea. For more information, see http://www.delorie.com/howto/cygwin/mno-cygwin-howto.html. In Jeremiah, write a simple test program to illustrate the similarities and differences between the two. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> # include <stdio. h>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> int main ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> printf (" hello world \ n ");
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> return 0;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/>} execute gcc-o hello. c Under MinGW and Cygwin respectively
./Hello can run hello world in their respective environments.
However, if you run Cygwin/gcccompiled by hello.exe in cmd, you cannot find Cygwn1.dll.
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ053J-7.jpg "/>
When hello. c is compiled under cygwin, the parameter-mno-cygwin is added. Then, it can be correctly executed when running in cmd. Gcc-mno-cygwin-o hello. c
./Hello, the program compiled by gcc-mno-cygwin, does not need Cygwin1.dll. This is why Cygwin1.dll is not prompted when we run the compiled VLC. Because we added CC = "gcc-mno-cygwin" CXX = "g ++-mno-cygwin" to the configure-vlc.sh ". That's not to say that the gcc-mno-Cygwin of cygwin is the same as the gcc of MinGW? After using Jeremiah, we found that there are still some differences. For example, if I use gcc-mno-Cygwin in cygwin to compile Live555, an error will be reported, and compilation in MinGW will be fine. The specific reason is not clear yet. However, general programs can still be seen as equivalent.
3. Create dynamic and static libraries and call methods using GCC
If you are a Linux developer, this is simply too simple. My title is mainly for developers of VC ++. Because Jeremiah came into contact with some VC ++ developers and found that these friends are not very familiar with Linux-related things. Therefore, write a simple program here to illustrate how to create and call a Linux library. The main compiler is the Gcc of MinGW or the gcc-mno-Cygwin of cygwin. Create a folder named test in the current directory. The following two folders are created: lib and testlib. Mkdir test & cd test & mkdir lib testlib create add. h and add. c Under lib. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> // add. h
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif"/> int add (int, int); 650) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> // add. c
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> # include" add. h"
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> int add (int x, int y)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> return x + y;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/>}
Compile and generate static library gcc-c add. c
Ar crs libadd. a add. o Compile and generate the dynamic library gcc-shared-o libadd. dll add. c. In this way, the static library and dynamic library containing the add function are created. Create test. c Under testlib to call the library. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> // test. c
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> include <stdio. h>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> # include" add. h"
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> int main ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> printf (" result = % d \ n ", add (3, 5 ));
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif "/> return 0;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'align = "top" src = "http://www.bkjia.com/uploads/allimg/131228/1TJ03523-0.gif"/>} execute gcc-o test. c-I .. /lib-L .. /lib-ladd
./Test. The result is 8, but this is only the static database connected. What if I connect to the dynamic library? Developers of VC ++ all know how to call the dll. They need LoadLibrary and GetProcAddress to import the functions in the dll one by one and call them. Reference: http://tech.ddvip.com/2007-03/117395352621216.html)
But it is not used in Linux. Calling dynamic libraries is the same as static libraries. Jeremiah liked this method very much)
Run rm ../lib/libadd..
Gcc-o test. c-I ../lib-L ../lib-ladd
./Test
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1TJ02M1-26.jpg "/>
This is mainly because we did not tell the system about the location of the dynamic library. Therefore, when the system calls libadd. dll, it cannot find its location.
In Linux, you can configure LD_LIBRARY_PATH. Export LD_LIBRARY_PATH = ~ /Test/lib but it is not easy to configure this environment variable in MinGW and Cygwin. It will still prompt that libadd. dll cannot be found. The environment variable to be configured is PATH. Export PATH = ~ /Test/lib: $ {PATH}
Echo $ PATH
./Test to get the result.
4. The static/dynamic libraries created by VC ++ are replaced with those created by GCC.
Can this happen? Is that true? The answer is yes.
Jeremiah suddenly wanted to study the issue of mutual calling between VC ++ and MinGW/Gcc, because the two environments were compiled with Windows programs, there must be some commonalities. The relationship between VC ++ static library. lib and MinGW/Gcc static library..
After research, the conclusion is:
1) the dynamic library libadd. dll generated by MinGW compilation can be called by any environment of VC ++.
2) The static library libadd generated by Mingw compilation. a can also be called by any environment of VC ++, but you only need to manually change the name of libadd. change a to libadd. lib.
3) add. h and add. c Using VC to create a static library and a dynamic library is a piece of cake for VC ++ developers. However, tests have found that only libadd generated by VC6 is used. lib and libadd. dll can be called by MinGW/Gcc. Other VS versions were not successfully debugged, mainly because MinGW/Gcc and VC6 both use COFF format, while other VS versions do not know the format. Can you adjust the format to this? Please let me know with VC ++. Thank you. The final result is that the dll of the VLC can be called in VC ++. In the next article, Jeremiah will introduce how to use VC ++ to call libvlc. dll to create an MFC player.
This article is from the steam media space blog, please be sure to keep this source http://jeremiah.blog.51cto.com/539865/183560