Problem with separate compilation

This is a discussion on Problem with separate compilation within the C++ Programming forums, part of the General Programming Boards category; Right well I decided to make one of the DOS programs i had written into a fully fledged Win32 app. ...

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420

    Problem with separate compilation

    Right well I decided to make one of the DOS programs i had written into a fully fledged Win32 app.

    Since windows code is large enough and the actual program code is pretty big i thought it wise to separate the actual program code and place it in another file.

    Thing is i can't get it to compile.

    I placed the function prototypes (with their relevant includes) in a header file, along with a structure definition, I put the code for the various functions (and their releveant includes) into a .cpp file of the same name. Then I included the header file (with "") in my main source file.......

    well i get a wacky error:

    Link Error : Undefined symbol: ?string_analyse@@YA?AUanalysis@@V?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z (struct analysis string_analyse(class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>, class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>)) in
    HelloWin32temp.cp

    I must be doing something wrong here, but i can't figure out what, if I copy the source code directly into the main file and replace the include for my header file with the includes needed for the functions, then it works.

    Any ideas?
    Last edited by Clyde; 05-17-2002 at 01:36 PM.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Perhaps a wrong sequence of including headerfiles. What compiler are you using? Perhaps it is a wrong sequence of linking.

  3. #3
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    I havent done this separate compilation stuff before so i must be making a stupid mistake, i've just tried makeing a header for a single function and using the function in my main file I get basically the same error. I tried it in both Codewarrior and MSVC++ both give me the same thing.

    Heres my header file (split.h):

    Code:
    #ifndef String_split_Guard
    #define	String_split_Guard
    
    #include <string>
    #include <vector>
    
    vector<string> split (const string& s);
    
    #endif
    Heres the corresponding source file (split.cpp)

    Code:
    #include <vector>
    #include <string>
    #include <cctype>
    
    using namespace std;
    
    vector<string> split (const string& s) // function that splits a string and inserts the individual
    {									   // words into a vector<string>
         vector<string> ret;
         typedef string::size_type string_size;
         string_size i = 0;
         
         while (i != s.size())
         { 
              while (i != s.size() && isspace(s[i]))  // finds the first non-space char - will be at position i
                   i++;
              string_size j = i;
              
              while (j != s.size() && !isspace(s[j])) // finds the first space character after i - will be at position j
                   j++;
              if (i != j) // if any words have been found
              {
                   ret.push_back(s.substr(i, j-i)); // pushes back the word found between i and j
                   i = j;
              }            
         }
         return ret;
    }
    And here's the relevant code from my main file
    Code:
    #include <windows.h>
    #include <string>
    #include <vector>
    #include "Split.h"
    blah
    blah
    input30 = split (input20);
    blah
    blah
    I get this error in both MSCV and codewarrior:

    Link Error : Undefined symbol: ?split@@YA?AV?$vector@V?$basic_string@DU?$char_tra its@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$ basic_string@DU?$char_traits@D@std@@V?$allocator@D @2@@std@@@2@@std@@ABV?$basic_string@DU?$char_trait s@D@std@@V?$allocator@D@2@@2@@Z (class std::vector<class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>, class std::allocator<class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>>>> split(class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> const &)) in
    HelloWin32temp.cp

    What i am doing wrong?
    Last edited by Clyde; 05-17-2002 at 04:03 PM.

  4. #4
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    You need to include split.h in split.cpp

    hth,

  5. #5
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Ah... you had my hopes there, unfortuneately i still get the same error

  6. #6
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    Only other thing I can think of is your spelling of split.h vs. Split.h
    or what not.

  7. #7
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    yea i thought of that and made sure that they were both split, grrrrr!

    Is there some difference when dealing with multiple source files with a win32 app?

  8. #8
    Ted
    Ted is offline
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    Not that I'm aware of. If your code is not too large, attach it and
    I'll check it out and see what I can come up with.

  9. #9
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    I've simplified it further and I still have the same problem!

    split.h:

    Code:
    #ifndef String_split_Guard
    #define	String_split_Guard
    
    #include <string>
    #include <vector>
    
    std::vector<std::string> split (const std::string& s);
    
    #endif
    split.cpp:

    Code:
    #include <vector>
    #include <string>
    #include <cctype>
    #include "split.h"
    
    using namespace std;
    
    vector<string> split (const string& s) // function that splits a string and inserts the individual
    {									   // words into a vector<string>
         vector<string> ret;
         typedef string::size_type string_size;
         string_size i = 0;
         
         while (i != s.size())
         { 
              while (i != s.size() && isspace(s[i]))  // finds the first non-space char - will be at position i
                   i++;
              string_size j = i;
              
              while (j != s.size() && !isspace(s[j])) // finds the first space character after i - will be at position j
                   j++;
              if (i != j) // if any words have been found
              {
                   ret.push_back(s.substr(i, j-i)); // pushes back the word found between i and j
                   i = j;
              }            
         }
         return ret;
    }
    wintest.cpp (my main file):

    Code:
    // Wintest.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	string input1;
    	vector<string> input2;
    	input2 = split (input1);
    	return 0;
    }
    stdafx.h: (that weird header thing that MSVC seems to need)

    Code:
    // stdafx.h : include file for standard system include files,
    //  or project specific include files that are used frequently, but
    //      are changed infrequently
    //
    
    #if !defined(AFX_STDAFX_H__7EF3AE03_69E3_11D6_82E3_005004F6DA69__INCLUDED_)
    #define AFX_STDAFX_H__7EF3AE03_69E3_11D6_82E3_005004F6DA69__INCLUDED_
    
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    
    
    // TODO: reference additional headers your program requires here
    #include <string>
    #include <vector>
    #include "split.h"
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    
    #endif // !defined(AFX_STDAFX_H__7EF3AE03_69E3_11D6_82E3_005004F6DA69__INCLUDED_)
    If i don't use MSVC i don't have the stdafx.h header, and instead my includes are before the int main() function.

    I get the error whether in codewarrior or msvc:

    Wintest.obj : error LNK2001: unresolved external symbol "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class
    std::allocator<char> > > > __cdecl split(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?split@@YA?AV?$vector@V?$basic_string@DU?$char_tr aits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V? $basic_st
    ring@DU?$char_traits@D@std@@V?$allocator@D@2@@std@ @@2@@std@@ABV?$basic_string@DU?$char_traits@D@std@ @V?$allocator@D@2@@2@@Z)
    Debug/Wintest.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    It works fine if i just copy the split.cpp code into wintest.cpp (making sure I have the relevant includes in stdafx.h) What's going on??
    Last edited by Clyde; 05-17-2002 at 04:38 PM.

  10. #10
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I've removed the header-file. It seems to work now.

    If it doesn't work, try declaring the function explicitly as external in the same file where you have your main.

    extern vector<string> split (const string& s);

    I'm using VC++ 6.

  11. #11
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    Which header file have you removed? I'm not quite sure what you mean.

    I'll try the extern function thing though

    EDIT: no luck i'm using MSVC++ 6.0, (though i got it by downloading MS visual studio) but i get the same error in codewarrior so i figured it wasn't a compiler specific problem.

    I'm really confused!
    Last edited by Clyde; 05-17-2002 at 04:50 PM.

  12. #12
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I removed split.h, VC++ gave errors on that. The way the function was declared there was wrong.

  13. #13
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    You removed the include <split.h> line from split.cpp. (presumeably not from wintest.cpp)

    Just tried it...... same problem.. maybe a problem with my computer??

  14. #14
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,420
    It must be a problem with my computer, I just made this:

    hello.cpp (main file):
    Code:
    #include <iostream>
    #include "test.h"
    
    using namespace std; 	//introduces namespace std
    int main( void )
    {
    	int i;
    	i = getnumber();
    	cout << "This is a test";
    	
    	return 0;
    }
    test.h:
    Code:
    int getnumber( void );
    test.cpp:
    Code:
    int getnumber( void )
    {
    	int x;
    	x = 5;
    	return x;
    }
    Same error:

    Link Error : Undefined symbol: ?getnumber@@YAHXZ (int getnumber()) in
    hello.cpp

  15. #15
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    Works fine with me.

    - You have made a MSVC++ project?
    - You have added the source files to the source folder?
    - You have added the include file to the include folder?

    If you have done that, then I really don't know what is wrong.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 02:04 AM
  2. Compilation problem
    By OSDever in forum C++ Programming
    Replies: 10
    Last Post: 09-08-2005, 07:42 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 09:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21