Writing a program in 2 languages?

孤街醉人 提交于 2019-12-19 08:29:49

问题


Can a program be written in more than one programming language? The stuff I read on The Daily WTF seems to imply that big companies/organizations employ several different languages when building a large application. How does that work? I know from working with Django that dynamic web pages are often put together with a bunch of different languages (Python for the controllers, HTML + Django's templating language for the view, and SQL for the models), but what about programs, ie stuff that would turn into an .exe when compiled?


回答1:


It's quite common to build large applications using different langauges and technologies. Mixing languages comes in different ways.

First, compilers from different languages can produce compatible output that can be linked together. That can be .obj files from C, C++, Pascal and other languages compiling to native code. Or that can be .NET assemblies - you can effortlessly use an assembly written in any .NET family language from any other .NET family language assembly.

Then, there're various interoperability technologies. You could have some code wrapped as a COM object and consumed from an application in a different language. Or you could put code in different langauges into different programs and make them communicate via some interprocess communication technology like RPC. In the latter case they will not care at all how the other process works - they only send and receive messages - much like you don't care that your browser and the web server you read pages from are most likely written in different languages.




回答2:


If the compilers for the different languages can produce the same format of object files, the linker doesn't care what language they were compiled from.

Alternatively, you can compile a .dll from any language and it works the same.




回答3:


It is possible, although not very common. All languages are eventually compiled to assembly, so as long as each language uses the same ABI (calling conventions, etc.), modules compiled in different languages can be linked together. It's not common, though.

In addition, many programs use an extension language. For example, much of the user interface in World of Warcraft is written in the Lua extension language, even though the core WoW program is probably C++.




回答4:


Yes , you can certainly write programs in more than 1 language. What you need to do is modularize the program. For example write modules as DLL's. You can use different languages for each DLL if you want to as long as the interface between the DLL's is common.

I work on a program which has been in development for over 8 years. The program was developed in C++ years ago. Today, its .NET. The modularization is performed by building COM objects. This allows us to use a variety of languages and technologies for the same application. The oldest code I have is basic vanilla C and the newest is C# 3.5.




回答5:


The short answer is Yes, the long answer is:

Yes, by doing one or more of the following:

  1. Building libraries and linking them together at compile time.
  2. Building dynamic libraries and linking to them at run time so you would have an exe and some dlls.
  3. In the case of frameworks like .net it doesn't really matter the language as long as it's supported by the framework, since it will either compile in the same type of code or the framework will know how to run it.



回答6:


The above answers (that I've seen so far) are all correct. Something else that isn't mentioned though is that companies/organisations define "large application" rather differently than programmers sometimes do.

To the health service, a hospital medical information system is a "large application" even if it actually involves, say, a bunch of different web servers, databases, various custom scripts for integrating various third party products, specially written client programs for radiology diagnostics, different web interfaces for doctors and nurses, and SMS system for paging doctors when results have been uploaded, etc.

In other words, where you might think "large application = 1 big binary", an organisation might think "large application = 1 noun for a bunch of big IT contracts, regardless of how many binaries are involved".




回答7:


Yes, certainly. Trivially in Windows with .NET you could have C#, VB, and C++ in the same resulting assembly/DLL as these all generate IL object code.

When compiling to native on Windows or other platforms (Unix, Linux) many compilers generated object files in a standard/shared format. In that case, the linker can easily combine them into a shared library, static library, or final runtime (a.out or ELF binary in Unix terms).

This is often done as different programmers have different preferences and also since some language constructs lend themselves to better describe a specific kind of solution.




回答8:


Most programming languages have a means for interfacing with C. When an application is put together from multiple different languages, it may be done by creating C interfaces for each of the different components and connecting the C interfaces, or the different components may actually be compiled into separate applications that communicate with each other using sockets or other forms of interprocess communication.




回答9:


Near the stone age in programming, when everything was written in assembly language, compiled languages came along. After some pain, grief, and years, it was hesitatingly acknowledged that writing at least some portion of an application in a "high level language" such as FORTRAN was a good thing. But many modules remained in assembly which contained things FORTRAN didn't do well, such as CPU trickery, tiny code space requirements, etc.

In an single application I worked on in the 1980s, we developed in FORTRAN, PL/M, 8086 assembly, and C. The language choices were driven by historical momentum plus ever improving compilers and build tools.




回答10:


Today it is very unlikely that you can get away with only one language.

Look at Adobe. All popular Adobe products are now containing more and more Lua scripting. They started with Lightroom and now there technical business strategy is to write the basic routines in C/C++/Assember (you need this for fast SSE graphics) and all the GUI is glued together with Lua.




回答11:


Yes, it can be done. More often that not it is done because the target language doesn't have a certain feature or the said feature is easier to carry out in another language.




回答12:


Performance is another reason to mix languages in a single application. In interpreted languages speed can be orders of magnitude slower than C/C++. Applications can have CPU-intensive portions written in C/C++ while leaving much of the program logic in the higher-level language to get the best of both performance and programming ease.



来源:https://stackoverflow.com/questions/1886086/writing-a-program-in-2-languages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!