Jump to content

function name

RexLee

I was writing a program and I named my function see.

int see(){  }

But it gave me a linker error.When I tried main though it worked.

int main(){  }

Is there something special with the "main"?

Link to comment
Share on other sites

Link to post
Share on other sites

You really didn't give much information, but since you mention linkers, I'll assume this is C. Providing the error you're getting would also help.

 

There isn't anything wrong with naming a function as 'see', but a function named 'main' is required as it is the application's entry point.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm writing a program in c++ to read and write from a text file, but I separated them so I can put it in the main source code  to use.

#include <iostream>#include <fstream>int see(){    std::string name;    int age;    std::string birthday;    std::ifstream file;    file.open("data.txt");    file >> name;    std::cout << "Name: " << name << "\n";    file >> age;    std::cout << "Age: " << age << "\n";    file >> birthday;    std::cout << "Birthday (year/month/day): " << birthday << "\n";    file.close();    system("PAUSE");    return 0;}
Link to comment
Share on other sites

Link to post
Share on other sites

Okay, every C++ program needs a main function since that's where the program starts executing. In a program with just one function, there's no point in renaming it. If you're that desperate to, I guess you could do this:

int see() { /* Code here */ }int main() { return see(); }

But like I said, that's just messy.

 

C++ isn't one of my best languages though, so I don't know if there's a way to force the compiler to use a different function in place of main.

Link to comment
Share on other sites

Link to post
Share on other sites

But if there is a lot of main wouldn't the ide get confused which one it should use?Because some functions are on other source files.

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, yes, I was assuming that was the only file in the project. You've probably just misconfigured the linker, but without posting the error message I can't help you there.

Link to comment
Share on other sites

Link to post
Share on other sites

  [Linker error] undefined reference to `WinMain@16' 

 

  ld returned 1 exit status 

Link to comment
Share on other sites

Link to post
Share on other sites

please upload your full code.

CPU: i7 4770k | GPU: Sapphire 290 Tri-X OC | RAM: Corsair Vengeance LP 2x8GB | MTB: GA-Z87X-UD5HCOOLER: Noctua NH-D14 | PSU: Corsair 760i | CASE: Corsair 550D | DISPLAY:  BenQ XL2420TE


Firestrike scores - Graphics: 10781 Physics: 9448 Combined: 4289


"Nvidia, Fuck you" - Linus Torvald

Link to comment
Share on other sites

Link to post
Share on other sites

It's not finished yet.I only completed the top code.

Link to comment
Share on other sites

Link to post
Share on other sites

Do you have a function called main () or WinMain (...) anywhere in the code? If no, you need one. If you do, you mentioned multiple source files, so are they in the same project? What IDE are you using? In visual studio, which that looks like, the the left pane, all of the files should be within one "Source code" folder on the tree, and one of those files should contain a main () or WinMain() function. If you want to use multiple source files, you will need to link them with header files so that they can call one another's functions, but that's not the problem you're having.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Ok now I get it.But can someone explain the whole process of how to link other source files.I have read my book but it does not explain clearly how to do so (or maybe it's my problem).I am so confused right now.#ifndef,#define,#endif,

cc -o program program.c...... I have no idea what each of these do and I am more confused every time I search on google.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok now I get it.But can someone explain the whole process of how to link other source files.I have read my book but it does not explain clearly how to do so (or maybe it's my problem).I am so confused right now.#ifndef,#define,#endif,

cc -o program program.c...... I have no idea what each of these do and I am more confused every time I search on google.

You're using visual studio, right? If so, the cc -o stuff isn't relevant because its for another compiler, gcc I think.

Regarding the other stuff: When you include a header file, the code is, in effect, pasted into the source code where you included it. Therefore, if you declare a function in the header file and include that file in multiple source files, these functions will be, from the point of view of the compiler, defined multiple times, which will throw an error and prevent the compilation. In order to get around this, each header file made by you should go:

[b]include files[/b]#ifndef [i]unique identifier[/i]#define [i]unique identifier[/i][b]Your code[/b]#endif
Edit: apparently you can't use bbcodes inside code blocks. Ignore it for now but you can hopefully see what I mean.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

I'm using dev c++.So if had a central source code to use the functions I would have to write the other source codes using the above way?And when I include them in the central code I just use #include"File.h" right.But how would I access the functions (and what name would I use to access them) from the other source files from the central source file?

Link to comment
Share on other sites

Link to post
Share on other sites

You're basically right. To use functions from other files, instead of declaring functions by putting the "Forward declaration" at the top of the file, you put it in the protected (as shown in prev post) part of the header file. Then you call it like a normal function.

You might have to do something special with DevC++ to compile it, I really don't know, I've only ever used MS visual studio.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

So I tried to do this with a simpler program:

main.cpp

#include <iostream>#include "function square.h"int main(){    std::cout << "Enter number to be squared: ";    double x;    std::cin >> x;    std::cout << "x * x = " << Square(x) << std::endl;    system("PAUSE");    return 0;}

function square.h

#ifndef FUNCTION_SQUARE_H#define FUNCTION_SQUARE_Hint Square(int x);#endif

function square.cpp

int Square(int x)//also tried with main(int x) cause Square(int x) won't compile{    return x*x;}

It gives me this error: 

9 C:\Users\Rex\Documents\all c++ files\test function\main.cpp `Square' undeclared (first use this function) 

What should I do?

Link to comment
Share on other sites

Link to post
Share on other sites

And also function square.h would not compile (no errors).

Link to comment
Share on other sites

Link to post
Share on other sites

You need to include function square.h in function square.cpp . If you've already done this, I don't know, sorry.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

It is still giving me the same error.By the way do I change the Square to main in function square.cpp cause it won't compile unless I do.

Link to comment
Share on other sites

Link to post
Share on other sites

So I started just typing code and trying things and it worked can someone explain how it worked.

main.cpp

#include <iostream>#include "function square.cpp"#include "function square.h"int main(){    std::cout << "Enter number to be squared: ";    int x;    std::cin >> x;    std::cout << "x * x = " << Square(x) << std::endl;    system("PAUSE");    return 0;}

function square.h

#ifndef FUNCTION_SQUARE_H#define FUNCTION_SQUARE_Hint Square(int x);#endif

function square.cpp

#ifndef FUNCTION_SQUARE_H#define FUNCTION_SQUARE_H#include "function square.h"int Square(int x){    return x*x;}#endif
Link to comment
Share on other sites

Link to post
Share on other sites

Turns out I don't even need the function square header file, I just had to include the the function square.cpp. What are header files used for anyways?

Link to comment
Share on other sites

Link to post
Share on other sites

What is the difference between a .cpp and .h?

Link to comment
Share on other sites

Link to post
Share on other sites

Changing it to main will be due to a compiler issue. I can't help you with that, but google probably could. You don't need to protect the .cpp files with the define stuff, and you shouldn't need to include the cpp file. When the compiler works properly, that may solve the problems. Unless I have the wrong information, which I don't think so, but it's possible.

In theory, a cpp (or c) file contains the source code and an h (or hh) file contains declarations and low level stuff I THINK. Also, you use h for stuff you use frequently, and usually when it's not project-specific, and cpp for the main code in your project.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

 

So I started just typing code and trying things and it worked can someone explain how it worked.

main.cpp

#include <iostream>#include "function square.cpp"#include "function square.h"int main(){    std::cout << "Enter number to be squared: ";    int x;    std::cin >> x;    std::cout << "x * x = " << Square(x) << std::endl;    system("PAUSE");    return 0;}

function square.h

#ifndef FUNCTION_SQUARE_H#define FUNCTION_SQUARE_Hint Square(int x);#endif

function square.cpp

#ifndef FUNCTION_SQUARE_H#define FUNCTION_SQUARE_H#include "function square.h"int Square(int x){    return x*x;}#endif

 

You should not include .cpp files and it is also not advisable to have spaces in file or directory names. Hence, the square cpp file does not need the ifndefs but it is still a good idea to include its .h file.

 

In this case you want to #include the square header file in your main file and you want to instruct your compiler to include both .cpp files in the compilation process.

For each file the compiler will start by replacing the #includes with the contents of those files and then compile the code to an object file. When all specified files have been compiled the linker will then generate the executable and take care of "replacing" the references to the external function square (and any others like printf, etc.) with calls to the address of the code that will be "merged" to the executable.

If you only compile the main file there will be an external reference to the function square but the linker does not know where to find it and won't produce the executable.

 

The use of include files makes it easier to share code among programs and tells the compiler that functions exist without having to specify their body (that is why you only put function declarations in header files and sometimes other structs or even macros). The linker is responsible for including all necessary code in the executable and, well, linking it. This is particularly useful in large applications where you can even specify a header file that #includes a bunch of other header files and you need only include that one in other cpp files.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×