简体   繁体   English

编译斯坦福C ++库

[英]Compiling the Stanford C++ library

http://www.stanford.edu/class/cs106b/assignments/Assignment1-linux.zip http://www.stanford.edu/class/cs106b/assignments/Assignment1-linux.zip

I am self-studying this assignment for an upcoming Coursera course. 我正在为即将到来的Coursera课程自学这项作业。 I modified Warmup.cpp in 0-Warmup folder as following: 我在0-Warmup文件夹中修改了Warmup.cpp,如下所示:

#include <iostream>
#include <string>
#include "StanfordCPPLib/console.h"
using namespace std;

/* Constants */

const int HASH_SEED = 5381;               /* Starting point for first cycle */
const int HASH_MULTIPLIER = 33;           /* Multiplier for each cycle      */
const int HASH_MASK = unsigned(-1) >> 1;  /* All 1 bits except the sign     */

/* Function prototypes */

int hashCode(string key);

/* Main program to test the hash function */

int main() {
   string name;
   cout << "Please enter your name: "; 
   getline(cin, name); 
   int code = hashCode(name);
   cout << "The hash code for your name is " << code << "." << endl;
   return 0;
}
int hashCode(string str) {
   unsigned hash = HASH_SEED;
   int nchars = str.length();
   for (int i = 0; i < nchars; i++) {
      hash = HASH_MULTIPLIER * hash + str[i];
   }
   return (hash & HASH_MASK);
}

It gives me this error: 它给了我这个错误:

andre@ubuntu-Andre:~/Working/Assignment1-linux/0-Warmup$ g++ Warmup.cpp -o a
/tmp/ccawOOKW.o: In function `main':
Warmup.cpp:(.text+0xb): undefined reference to `_mainFlags'
Warmup.cpp:(.text+0x21): undefined reference to `startupMain(int, char**)'
collect2: ld returned 1 exit status

What is wrong here? 怎么了


UPDATE: Got this to work now. 更新:现在就可以使用了。

1. cd to the folder containing assignment.cpp
2. g++ assignment.cpp StanfordCPPLib/*.cpp -o a -lpthread

StanfordCPPLib/*.cpp this part indicate that everything in the library will be compiled,
-pthread will link pthread.h, which is used by several utilities in the Stanford library.

A random "StanfordCPPLib/console.h" I found on the net includes fun code like: 我在网上发现的随机"StanfordCPPLib/console.h"包含有趣的代码,例如:

#if CONSOLE_FLAG | GRAPHICS_FLAG

#define main main(int argc, char **argv) { \
extern int _mainFlags; \
_mainFlags = GRAPHICS_FLAG + CONSOLE_FLAG; \
return startupMain(argc, argv); \
} \
int Main

extern int startupMain(int argc, char **argv);

So your main() function is apparently really a function called Main() that is ultimately called by some support function, startupMain() , that's in a library your instructor should provide. 因此,您的main()函数显然确实是一个称为Main()函数, startupMain()函数最终由某些支持函数startupMain()调用, startupMain()函数位于讲师应提供的库中。

You need to link to that library. 您需要链接到该库。 Your assignment or course notes should have instructions on how to do that and where the library comes from. 您的作业或课程笔记应包含有关操作方法以及库的来源的说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM