"unresolved external symbol _triangulate" when using triangle library - c++

I'm currently using the triangle library in my program. The library contains only .c and .h files (no .lib). I get the following error on Visual Studio C++ 2010:
1>data.obj : error LNK2019: unresolved external symbol _triangulate referenced in function "struct triangulateio __cdecl readfile(void)" (?readfile##YA?AUtriangulateio##XZ)
The header file of my data.cpp is the following:
#ifndef DATA_H
#define DATA_H
#include <WinSock2.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include <time.h>
#include <GL/gl.h> //include the gl header file
#include <GL/glut.h> //include the glut header file
#include <GL/glu.h> //include the glut header file
#include <armadillo>
//Namespace
using namespace std;
using namespace arma;
extern "C"
{
#ifdef SINGLE
#define REAL float
#else /* not SINGLE */
#define REAL double
#endif /* not SINGLE */
#include "triangle.h"
}
triangulateio readfile();
#endif
Data.cpp
triangulate("pczAevn", &in, &mid, &vorout);
I've already made my program work with a Makefile of mine on Ubuntu, but I need to run my program on windows.
Feel free to ask for more information.
EDIT #1:
If you use the triangle library with VS, you have to put the following instruction on top of the triangle.c file #define TRILIBRARY
Now it compile. Thank you very much for the help.

The linker can't find a definition for "triangulateio readfile()", if it's defined in the .c file my guess is that it isn't built. If you include it in the project it could work.

Related

How to properly include the same .h file in two separate .cpp files?

I have a project consisting of 6 files; main.cpp, functions.h, tennisplayer.h, tennisplayer.cpp, tennisteam.h & tennisteam.cpp which are roughly defined as follows:
// main.cpp
#include "tennisteam.h"
#include <iostream>
#include <string>
#include <exception>
// some main() code that needs functions.h definitions
// functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#include "tennisplayer.h"
#include <iostream>
#include <string>
#include <limits>
// some constants & function definitions needed by both main.cpp & tennisteam.cpp
#endif
// tennisplayer.h
#ifndef TENNISPLAYER_H
#define TENNISPLAYER_H
#include <string>
#include <vector>
// tennisplayer class declarations
#endif
// tennisplayer.cpp
#include "tennisplayer.h"
#include <iostream>
#include <fstream>
// tennisplayer class definitions
// tennisteam.h
#ifndef TENNISTEAM_H
#define TENNISTEAM_H
#include "tennisplayer.h"
#include <string>
#include <vector>
//
#endif
// tennisteam.cpp
#include "tennisteam.h"
#include <iostream>
#include <fstream>
// tennisteam class definitions
However, when I include functions.h into both main.cpp & tennisteam.cpp via tennisteam.h I get a linker error along the lines of:
/usr/lib/gcc/x86_64-pc-cygwin/11/../../../../x86_64-pc-cygwin/bin/ld: /tmp/ccv30cX0.o:tennisteam.cpp:(.text+0x0): multiple definition of `function(std::string const&)'; /tmp/ccRThgpp.o:main.cpp:(.text+0x0): first defined here
I'm aware this is a linker error. I've looked around for a fix but all I come across are posts instructing me to use include guards which I have done already. Is there something I'm missing here? Any help would be appreciated.
You have function function(std::string const&) that you not only declared but also defined in your header file. If you need to have it defined there instead of a .cpp file, mark it as inline.
This results in two cpp files (namely main.cpp and tennisteam.cpp) ending up with a definition of that function, because they both include that header file.

CLion C++ fatal error: 'process.h' file not found

When I use CLion on a Mac to compile C++ code for highlight removal in a single image, there is an error:
Please help me fix it.
#ifndef QX_CVPR09_CTBF_BASIC_H
#define QX_CVPR09_CTBF_BASIC_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numeric>
#include <vector>
#include <process.h>
#include <direct.h>
#include <io.h>
#include <time.h>
#include <string>
#include <memory.h>
#include <algorithm>
#include <functional> // For greater<int>()
#include <iostream>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
#define QX_DEF_PADDING 10
#define QX_DEF_THRESHOLD_ZERO 1e-6
class qx_timer {public: void start(); float stop(); void time_display(char *disp=""); void fps_display(char *disp=""); private: clock_t m_begin; clock_t m_end;};
It's a part of my code. The full code is too long.
process.h
process.h is a C header file which contains function declarations and
macros used in working with threads and processes. Most C compilers
that target DOS, Windows 3.1x, Win32, OS/2, Novell NetWare or DOS
extenders supply this header and the library functions in their C
library. Neither the header file nor most of the functions are defined
by either the ANSI/ISO C standard or by POSIX.
Depends on which platform you compile and what standard you use. If you are on linux or compile with c99/ansi standard then this header will probably just not be available (which might be your error)

Why do I get so many `LNK2005` errors despite using `#ifndef #define #endif` code blocks in my .h files?

I have 21 files as shown here in the picture:
The following 35 functions are declared in lines 364-411 of util.h inside a
#ifndef FlagUtil
#define FlagUtil
#endif
code block:
Create_Pix, Remove_Pix, my_round, edit_error, check_file, check_dir,
read_config, write_config, write_envi_config, my_randomize, my_random,
my_eps_random, cadd, csub, cmul, cdiv, cpwr, cconj, cimg, crel, cmod, cmod2,
angle, cplx_sinc, PolTypeConfig, init_file_name, memory_alloc, PrintfLine,
CreateUsageHelpDataFormat, CreateUsageHelpDataFormatInput,
init_matrix_block, block_alloc, CheckFreeMemory, CheckFreeMemoryWin32,
CheckFreeMemoryLinux
The following 30 functions are declared in lines 99-135 of util_block.h inside a
#ifndef FlagUtilBlock
#define FlagUtilBlock
#endif
code block:
read_matrix_int, read_matrix_float, read_matrix_cmplx, write_matrix_int,
write_matrix_float, write_matrix_cmplx, read_matrix3d_float,
read_matrix3d_cmplx, write_matrix3d_float, write_matrix3d_cmplx,
read_block_matrix_int, read_block_matrix_float,
read_block_matrix_matrix3d_float, read_block_matrix_cmplx,
write_block_matrix_int, write_block_matrix_float,
write_block_matrix_matrix3d_float, write_block_matrix_cmplx,
write_block_matrix3d_float, write_block_matrix3d_cmplx, read_block_S2_avg,
read_block_S2_noavg, read_block_S2T6_avg, read_block_SPP_avg,
read_block_SPP_noavg, read_block_TCI_avg, read_block_TCI_noavg,
read_block_S2_TCIelt_noavg, read_block_SPP_TCIelt_noavg, average_TCI
The following 35 functions are declared in lines 106-149 of util_convert.h inside a
#ifndef FlagUtilConvert
#define FlagUtilConvert
#endif
code block:
S2_to_C3elt, S2_to_C4elt, S2_to_T3elt, S2_to_T4elt, S2_to_T6elt,
SPP_to_C2elt, SPP_to_T2elt, S2_to_SPP, S2_to_IPP, S2_to_C2, S2_to_C3,
S2_to_C4, S2_to_T2, S2_to_T3, S2_to_T4, S2_to_T6, SPP_to_C2, SPP_to_T2,
SPP_to_IPP, SPP_to_T4, C2_to_IPP, C2_to_T2, T2_to_C2, C4_to_T4, C4_to_C3,
C4_to_T3, C4_to_C2, C4_to_IPP, T4_to_C4, T4_to_C3, T4_to_T3, C3_to_T3,
C3_to_C2, C3_to_IPP, T3_to_C3, T6_to_C3
And the contents of the file PolSARproLib.h is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif
/* ROUTINES DECLARATION */
#include "util.h"
#include "util_block.h"
#include "util_convert.h"
#include "graphics.h"
#include "matrix.h"
#include "processing.h"
#include "statistics.h"
#include "sub_aperture.h"
#include "my_utils.h"
And the contents of the file PolSARproLib.c is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif
/* ROUTINES DECLARATION */
#include "util.c"
#include "util_block.c"
#include "util_convert.c"
#include "graphics.c"
#include "matrix.c"
#include "processing.c"
#include "statistics.c"
#include "sub_aperture.c"
#include "my_utils.c"
I’ve placed those 21 files in a folder named Static Project and I’ve created the PolSARproLib.lib file as follows, my machine is a Win 10x64 one: (here I’ve uploaded this project):
The files graphics.obj, matrix.obj, my_utils.obj, PolSARproLib.lib, PolSARproLib.obj, processing.obj, statistics.obj, sub_aperture.obj, util.obj, util_block.obj, util_convert.obj are created in the Debug folder of the project without any error, but some warnings like:
1>graphics.obj : warning LNK4006: _write_header_bmp_8bit already defined in
PolSARproLib.obj; second definition ignored
I have created a folder named Arii and placed the following 22 files in the subfolder \Arii\lib
I have also the file arii_anned_3components_decomposition.c placed in the folder Arii:
Now I create the project arii_anned_3components_decomposition in the folder Arii as follows (here I’ve uploaded the project)
In the following 2 pictures, you see the configuration of the project. Again, I should say that I’m working on a Win 10x64 machine
External library is added to the project as follows:
Properties>C/C++>General>Additional Include directories
I enter the path to the folder in which header files .h are placed
Properties>Linker>General>Additional library directories
I enter the path to the folder in which .lib files are placed
In
Properties>Linker>Input>Additional Dependencies
I’ve entered name of those needed .lib files:
Here is the beginning (before main function) of the file arii_anned_3components_decomposition.c:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "omp.h"
#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif
/* ROUTINES DECLARATION */
#include "../lib/PolSARproLib.h"
Now I build the solution:
And unfortunately I get 104 errors. 100 of them is about multiple definition of the functions in util.h, util_convert.h, util_block.h (those functions are listed in the beginning of question)
Errors 1-100 are alike:
1>PolSARproLib.lib(PolSARproLib.obj) : error LNK2005: _C3_to_T3 already
defined in PolSARproLib.lib(util_convert.obj)
Here I’ve uploaded the error and warning file.txt
I really don’t understand why am I getting these errors regarding that the code blocks #ifndef #define #endif have been used in the
code?
As stated by CoryKramer, we don't need the file PolSARpro.c, in fact we should never #include .cpp or .c files and this is what has been done in that file:
The contents of the file PolSARproLib.c is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <time.h>
#ifdef _WIN32
#include <dos.h>
#include <conio.h>
#endif
/* ROUTINES DECLARATION */
#include "util.c"
#include "util_block.c"
#include "util_convert.c"
#include "graphics.c"
#include "matrix.c"
#include "processing.c"
#include "statistics.c"
#include "sub_aperture.c"
#include "my_utils.c"
So, in order to get rid of all those 100 LNK2005 errors, simply place these 20 files (Omit PolSARproLib.c) in a folder
And do the next steps as stated in the question for creating and building the static library project PolSARproLib.lib file and for creating and building the console application project arii_anned_3components_decomposition.exe file.

Error LNK2005: function already defined in MyForm.obj

I'm trying to make a windows form application in Visual Studio C++, but I get this errors after compiling, for each function:
error LNK2005: function already defined in MyForm.obj
These are my files:
Source.cpp
#pragma once
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]//leave this as is
void main() {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Project1::MyForm);
}
MyForm.h
#pragma once
#include <iostream>
#include <string>
#include "Header.h"
namespace Project1 {
//codes of te form
}
Header.h
#pragma once
#include <iostream>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int n, m;
int size1, size2;
//My functions here
So how can I fix errors?
If you declare and implement a function in a header file (Header.h) and if this file gets included twice, then you'll most likely will get a function already defined error at some point.
This can be fixed by either:
Moving function implementation to a source (cpp) file and only keep it's declaration in the header (h) file
Make the function inline (if it is acceptable), that will remove the error

What is causing ld: duplicate symbol

ld: duplicate symbol StringFunctions::intToString(int) in
/Build/Intermediates/Y36PJC-mrvikmil.build/Debug/Y36PJC-mrvikmil.build/Objects-normal/x86_64/ServerSocket.o and
/Build/Intermediates/Y36PJC-mrvikmil.build/Debug/Y36PJC-mrvikmil.build/Objects-normal/x86_64/main.o
for architecture x86_64
function StringFunctions::intToString(int) is in StringFunctions.h
includes from main.cpp:
#include <iostream>
#include <string>
#include "Exception.h" //does not include anything more
#include "ServerConsole.h"
/*
which includes ServerSocket.h which includes ClientSocket.h which includes StringFunctions.h
AND
ServerSocket.h includes SocketException.h which includes StringFunctions.h
*/
includes from ServerSocket.cpp:
#include <iostream>
#include <string>
#include "InvalidPortException.h" //does not include anything more
#include "SocketException.h" //which includes StringFunctions.h
#include "ClientSocket.h" //which includes SocketException.h which includes StringFunctions.h
#include "StringFunctions.h" //StringFunctions.h
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <errno.h>
I believe that as long as i have things in .h files, i can include pretty much anything anywhere...
All my files has include guard (#ifndef ... #define ...)
Please help.
You have to define the function as inline or move its implementation to a cpp file. Otherwise it will exist in both objects and C++ won't know which one (despite them being identical) to link.
Include guards will avoid having the same header multiple times in the same code file/object. However they won't avoid having the same piece of code in multiple object files, as each one is create on its own with all defines reset.