Compile a project containing multiple main functions in eclipse CDT

Source: Internet
Author: User

Http://weblog.imthewan.com/2010/09/eclipse-cdt-multiple-target/

In a C/C ++ project, you sometimes need to include multiple main functions in a project. In Visual Studio, this problem can be solved by bringing multiple projects under a solution, but there is no such concept in eclipse CDT. In fact, through the customized makefile, eclipse CDT can easily achieve the goal, for those who are used to makefileProgramPersonnel may think this is more flexible. By default, eclipse CDT will automatically generate makefile for both the debug and release configurations of the project, in addition, this automatically generated makefile will try to compile all the source files and generate a separate binary file with the project name command. If more than two files in your project contain the main function, eclipse will report an error:

Multiple definition of 'main'

Since eclipse also uses makefile, you must be able to modify or create makefile by yourself so that eclipse can know when to dare and what to do, instead of doing what you want. After some attempts, I found a feasible path. The following is a simple example to illustrate how to use makefile freely in eclipse.

Suppose we have a very simple C/S structure project, which includes the client and server to be compiled. Now you have writtenCode: Tcp_server.cpp, tcp_client.cpp. Let's do it now!

Step 1: locate "C/C ++ build" in Project Properties and click "manage deployments…" on the right panel ...", Then select "New ..." Create a new configuration (Figure 1), name it mydebug, and copy it from the existing debug configuration (figure 2 ). Return to the window in Figure 1 and select mydebug from the drop-down menu at the top ." In the makefile generation area, there is an option "generate makefiles automatically", because we have to write it by ourselves, so uncheck this option. Then you can select "build directory". For simplicity, it is recommended to set it as the root directory of the Project. Note that this must be the path where you store makefile.

Step 2: Write makefile. Place it in the path set above. If the directory is not the same as the code file, use the ".." Operator in makefile to jump to the path. The makefile I used in this example is as follows:

12345678910111213141516 # demo multiple target project # fwoncn CC: = g ++ RM: = RM -Rf libs: =-l boost_system-l boost_thread All: tcp_client tcp_server clean: $ (RM) tcp_client tcp_server tcp_client: tcp_client.cpp $ (CC) tcp_client.cpp-O tcp_client $ (libs) tcp_server: tcp_server.cpp $ (CC) tcp_server.cpp-O tcp_server $ (libs)

Step 3: compile the project. If you select "build automatically", you don't have to worry about it. eclipse will work based on your makefile and there will be no more main function errors. Otherwise, click the drop-down button next to the build icon on the toolbar and select mydebug. eclipse will find the first target defined in makefile, that is, all, to compile two pairs respectively. In addition, if you want to compile a specific target separately, you can select the project menu-make target-build... -Add... The name is the same as the target name in makefile, such as tcp_client. Then select the newly created target and click build.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.