| | |
Inline question
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
As I was busy inlining some of my functions, I was wondering if it is really necessary to inline anything. Doesn't the compiler automatically inline stuff, or do I have false memories? And if the compiler does inline stuff for me, would it be best to let it do its job or should I mess up something by doing it myself?
The reason I thought of the first question is because I can't get my now-inlined functions to work. A quick example:
test.cpp
test2.cpp
test.h
This generates the error:
Removing the inline will make the program work.
The reason I thought of the first question is because I can't get my now-inlined functions to work. A quick example:
test.cpp
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include "test.h" void main() { std::cout << getString(); std::cin.get(); }
test2.cpp
c++ Syntax (Toggle Plain Text)
#include "test.h" inline std::string getString() { return "Beam me up, Scotty!"; }
test.h
c++ Syntax (Toggle Plain Text)
#ifndef TEST_H #define TEST_H #include <string> std::string getString(); #endif
This generates the error:
C++ Syntax (Toggle Plain Text)
test.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getString(void)" (?getString@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main C:\w\e\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals
Removing the inline will make the program work.
Compilers are free to choose to ignore the inline keyword or not.
Move the code in test2.cpp to test2.h since you use the linline keyword, then delete test2.cpp from the project.
Move the code in test2.cpp to test2.h since you use the linline keyword, then delete test2.cpp from the project.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
The function definition is not being included in your test.cpp module, so the linker can't find it. Often inline function definitions are saved as a different type of file (like test.inl) which is itself #include -ed.
if you really need to inline something and aren't sure whether the compiler will or not, standard practice is to write the function definition in the function declaration. In other words move the code into test.h as follows:
or include the code module as well...usually instead of being saved as a .cpp the file is saved as another type (often .inl) which you will also have to #include.
As to whether it's worth doing, that depends. If it is absolutely time-critical code and you are unsure as to whether the compiler will or will not inline functions, and it is worth the code bloat, sure. Otherwise, letting the compiler decide when to inline code...and with compilers you can change how aggressive they are about inlining code, also. I can't really see the need for it in a function like the one you mentioned...it's more often seen when writing very time critical math functions and such.
if you really need to inline something and aren't sure whether the compiler will or not, standard practice is to write the function definition in the function declaration. In other words move the code into test.h as follows:
C++ Syntax (Toggle Plain Text)
#ifndef TEST_H #define TEST_H #include <string> std::string getString(){ return "Beam me up, Scotty!"; }; #endif
or include the code module as well...usually instead of being saved as a .cpp the file is saved as another type (often .inl) which you will also have to #include.
As to whether it's worth doing, that depends. If it is absolutely time-critical code and you are unsure as to whether the compiler will or will not inline functions, and it is worth the code bloat, sure. Otherwise, letting the compiler decide when to inline code...and with compilers you can change how aggressive they are about inlining code, also. I can't really see the need for it in a function like the one you mentioned...it's more often seen when writing very time critical math functions and such.
![]() |
Similar Threads
- Inline Assembly Help Please! (Assembly)
- Lisp question (Legacy and Other Languages)
- Help with vector.. simple question (C++)
- C++ Tic Tac Toe Question & Help (Game Development)
- A question about RAM... (Motherboards, CPUs and RAM)
- Stupid question(using class object) (C++)
- newbie question about vbulletin (Social Media and Online Communities)
- how do i use inline c or C++ in visual basic net? (VB.NET)
Other Threads in the C++ Forum
- Previous Thread: my program-calculator
- Next Thread: calculator and loop problem next
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg simple sorting string strings temperature template text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






