开发者

A couple of Eclipse C/C++ questions

I've been using VS2008/2010 for a while and I'm going to learn using Eclipse Helions for C/C++ development (mainly C).

I am abit confused about libraries and includes though.

1) How do you properly include winsock2.h for example? I've tried this:

#ifndef CONFIG_H_
#define CONFIG_H_


/*  Windows-Build   */
#if defined(WIN32) || defined(_WIN32)
#include <winsock2.h>
#endif

SOCKET sock;

#endif /* CONFIG_H_ */

But the compiler dosn't recognise SOCKET. Do you have to manually add the full path to winsock2 somewhere?

2) What about ws2_32.lib? Where do you include that in Eclipse? Do you have to add a path as well?

3) Having used VS mostly I'm new to makefiles. How do you include custom makefiles? Is there a good guide for starting with makefiles?

4) Is there a intellisense like in VS?

That's what I can figure out right now. Thanks!开发者_C百科

EDIT: In response to the first answer:

Building target: Filesharing_core.dll
Invoking: Cygwin C Linker
gcc -L"C:\cygwin\lib\w32api" -shared -o"Filesharing_core.dll"  ./src/test.o   -llibws2_32.a
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -llibws2_32.a
collect2: ld returned 1 exit status
make: *** [Filesharing_core.dll] Error 1


First off, cygwin will usually use GCC as it's compiler. That means that there's no special windows support that you'll find in the VS compiler and editor. Be prepared to get your hands dirty.

Keep in mind that my CDT version is a bit old and I don't have Cygwin installed so some of the things below might not be accurate. Also all the compiler options that I mention are detailled in the gcc manual.

1) In your sample, nobody is defining WIN32 or _WIN32 (I don't think GCC is going to do that for you but do check). To fix that, you'll have to include the windows.h header which (I think) will take care of definning the correct macros. You could also use the -D compiler switch (configurable in your makefile or through the eclipse menus).

If you encounter missing include errors once you've fixed the defines, I believe that the windows headers are located in the C:\cygwin\usr\include\w32api folder. To add that to your include path, simply open your project properties and navigate to C/C++ General > Paths and Symbols. Add the path to the GNU C group in the Includes tabs. Depending on how you configured your project, this might have already been done for you.

If you're building with your own makefile, you should still do the previous step because it will allow the indexer to find and parse those headers. To tell the compiler about the include folder, use the -I switch.

2) I'm going to guess that ws2_32.lib is the lib file for winsock2. If this is the case, I'm not entirely sure who's responsible for building it (is there a .dll you can use instead?). You might want to check your c:\cygwin\usr\lib folder or the c:\cygwin\usr\local\lib.

If you're using a managed project (eclipse builds the makefile for you) then go to your project properties and navigate to C/C++ Build > Settings. In the Tool Settings tab, go to the libraries item in the linker section. Just add the name of the lib file and the folder in the appropriate boxes.

In your own makefile you'll want to use the -l compiler switch to specify a library and the -L compiler switch to specify a search path.

3) A good place to get started with makefiles would be the GNU make manual.

One detail about running a Makefile on Windows: make sure to use the shell provided by Cygwin or MinGW. Otherwise, commands like rm won't be defined and it'll make your life very difficult.

You might also want to consider CMake. It's easier to use and scales better to larger projects.

To use a hand-made makefile in your project, just create an new Makefile project and dump your Makefile file in the root of your project folder. That's it.

4) Yes there is but it's not called intellisense. Just hit CTRL+Space anywhere in your source code to bring it up.

Other fun tools can be found in the right-click menu. My personal favorites include CTRL+SHIFT+R to find and open a file, CTRL+SHIFT+T to find and open a type\variable\function\define and CTRL+O to find and goto a type\variable\function\define within the opened file.

The indexer can go a little crazy sometimes (mostly when parsing C++ code). You can modify its behaviour by going in the Windows > Preferences menu at the top and navigating to the C/C++ > Indexer item.

I hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜