Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7664099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:13:05+00:00 2026-05-31T14:13:05+00:00

Does anyone have ideas about checking the existence of the main function in a

  • 0

Does anyone have ideas about checking the existence of the main function in a c++ source, as I want to write a somewhat automatic makefile, so that the c++ sources with main function will be linked while those without main function won’t link.

Lexical or grammar parsing may be not suitable for this simple task.

Any existing command line tools or libraries will be much helpful to this automatic task.

Thanks for any ideas!

The makefile file:

VPATH = include
CPPFLAGS += -I include
CFLAGS += -I include

C_SOURCE := $(shell find . -iname '*.c')
CPP_SOURCE := $(shell find . -iname '*.cpp')
D_OBJ := $(subst .cpp,.d, $(CPP_SOURCE))
EXE := $(subst .c,, $(C_SOURCE))
EXE += $(subst .cpp,, $(CPP_SOURCE))

.PHONY: all
all: $(EXE)

include $(D_OBJ) 

$(D_OBJ): %.d: %.cpp
    $(CC) -MM $(CPPFLAGS) $< > $@.temp;
    auto_depend_gen $@ "$@.temp" > $@; 
    rm -rf $@.temp

#print_msg:
#   @printf "$(EXE)\n"
#   @printf "$(D_OBJ)\n"

.PHONY: clean
clean:
    rm $(EXE) $(D_OBJ)

So this is an automatic dependency generation makefile. With this makefile, I need not modify the makefile every time I add a C++ source file. The headers are determined by the “gcc -MM” command and the object files I want to link share the same name with the header files except the suffixes. auto_depend_gen is a program written by myself which just removes the .o suffix of the first line of the file generated by “gcc -MM”.

Some source files have main function, while some do not, so comes this problem.

Maybe a more general question is: while a Java project can be built automatically with more than one of the .class files having a main function, but C++ cannot. So I just want to solve it.

Appreciate more comments!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:13:06+00:00Added an answer on May 31, 2026 at 2:13 pm

    You can use nm to list symbols in object file. Check if main is one of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone have any ideas about how to make a Javascript virtual keyboard for
Does anyone have any ideas about how I could go about randomising an list
Does anyone have any ideas on how to read the MSDTC configuration on the
Does anyone have any ideas how to get the Accordion control in the ASP.Net
Does anyone have any idea if you can find source JARs on Maven repositories?
Does anyone have any ideas on if/how one could create a regular expression to
Does anyone have any ideas on where the worst Global Hackers Live (from a
For a purely educational exercise: does anyone have any ideas or advice on how
Does anyone have any idea what is wrong with this create statement for mysql?
Does anyone have an idea how can I fix this vulnerability in Apache 2.2.4

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.